# Thread: Random Floating Point Numbers

1. ## Random Floating Point Numbers

Hi,

I am using
Code:
```srand(time(0));

rand() % 100;```
to create random values. This makes values like 18, 26 etc.
I need it to create random floating point values such as 18.6, 26.8 etc.

How would i get it to create floating point values not integers.

Thank you.

2. '(double)rand() / RAND_MAX' should give you a random floating point number between 0.0 and 1.0. Then you can just multiply it by your upper limit.

3. Thanks.

I am using INFp (positive infinity), INFn (negative infinity) and NAN (Not A number) in my program.
I want to include those in the random floating point values that are created. How would i do that?

Thanks

4. Reserve some numbers or range for them and translate them. Eg:
if (rnd == 10) rnd = inf;
etc.

Also, use nullptr, not 0.

Thanks.

I am using INFp (positive infinity), INFn (negative infinity) and NAN (Not A number) in my program.
I want to include those in the random floating point values that are created. How would i do that?

Thanks
My thoughs are that this is technically impossible. Given infinite set of numbers (so we can include +/-INF), chance of getting any number (including INFs) within this set is 0.
Reserving a fixed amount of integers for infinities gives chance near 0, because RAND_MAX can be any integer in <32767, INF).
I would set up own constant X, not greater than RAND_MAX and not greater than N. This would give a finite subset for X (the rand() divisor)
Or the second solution, reserve X% of upper numbers (of RAND_MAX) for special values. Remember to divide by 100-X% of RAND_MAX to get numbers along with 1.0.
Why do you want to generate 'not a number' from 'a set of numbers'? This does not make sense to me.

6. Hi, i am creating a sorting algorithm that sorts INFp, INFn and Nans.

I need to include those in the array that is sorted. So if i am creating random values i need those to be included aswell.

Is there anyway i can assign something to every say 5th element in the array. That way i can create a normal random array, and then assign some of the special cases into the array.

Thanks

7. What's wrong with a normal loop then? Loop through your array with a step of 5.

8. Hi, yes i thought of that, but i don't know how to do the step of 5.

Thanks

9. Hi, i'm trying something like this.

Code:
```	for (int i=0; i < numberofelements; i+5)
{
if (i < numberofelements-3)
{
Array[i] = INFp;
Array[i+1] = INFn;
Array[i+2] = NAN;
}
}```
Number of elements could be any number that the user enters.
I want to increment by 5, and then set the fifth element to INFp, the 6th as INFn, and the the as NAN.
Why is this not working.

Thanks

10. Well... That's almost it. "i+5" in your for loop should be "i += 5" or "i = i + 5".

11. Code:
```	for (int i=0; i < numberofelements - 2; i += 5)
{
Array[i] = INFp;
Array[i+1] = INFn;
Array[i+2] = NAN;
}```
Better.

12. Thank you works perfectly .

13. Originally Posted by itsme86
'(double)rand() / RAND_MAX' should give you a random floating point number between 0.0 and 1.0. Then you can just multiply it by your upper limit.
I tried doing

(double)rand() / RAND_MAX * 15. Using 15 as my upper limit. But i dont really have a upper limit, what is the highest i can set it to.

Also the values it creates are many digits, what could i do to make them like 13.2 instead of 13.2321. Or maybe have a mixture of some being like 13.2 and some being 13.356 etc.

Thanks.

I tried doing

(double)rand() / RAND_MAX * 15. Using 15 as my upper limit. But i dont really have a upper limit, what is the highest i can set it to.

Also the values it creates are many digits, what could i do to make them like 13.2 instead of 13.2321. Or maybe have a mixture of some being like 13.2 and some being 13.356 etc.

Thanks.
<0, 1) with 4 digits:
Code:
`(double)(rand() % 10000) / 10000`
<0, 1000) with 1 digit:
Code:
`(double)(rand() % 10000) / 10`

15. Thanks.

So for where it says 10000 can i put any number to make the range greater?.
Also i was wondering if it is possible to create minus values randomly.

Thanks.