# Random Floating Point Numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-25-2011
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.
• 02-25-2011
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.
• 02-26-2011
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
• 02-26-2011
Elysia
Reserve some numbers or range for them and translate them. Eg:
if (rnd == 10) rnd = inf;
etc.

Also, use nullptr, not 0.
• 02-26-2011
kmdv
Quote:

Originally Posted by Khadafi
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.
• 02-26-2011
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
• 02-26-2011
Xupicor
What's wrong with a normal loop then? Loop through your array with a step of 5.
• 02-26-2011
Hi, yes i thought of that, but i don't know how to do the step of 5.

Thanks
• 02-26-2011
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
• 02-26-2011
Xupicor
Well... That's almost it. "i+5" in your for loop should be "i += 5" or "i = i + 5". :)
• 02-26-2011
kmdv
Code:

```        for (int i=0; i < numberofelements - 2; i += 5)         {                 Array[i] = INFp;                 Array[i+1] = INFn;                 Array[i+2] = NAN;         }```
Better.
• 02-26-2011
Thank you works perfectly :).
• 02-26-2011
Quote:

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.
• 02-26-2011
kmdv
Quote:

Originally Posted by Khadafi
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`
• 02-26-2011