# Board index » cppbuilder » Random float number...

## Random float number...

2005-07-26 02:47:03 AM
cppbuilder18
Hello,
How can i make rand() return a float number between [0,1] ? (0 and 1
inclusive)
Paulo Dias

## Re:Random float number...

What precision of float do you need?
For example, you can make random number as integer
in speciffic region and then devide that number with
10, 100, ... etc, to get desired float.
--
Best regards,
"Paulo Dias" < XXXX@XXXXX.COM >wrote in message
##### Quote
Hello,

How can i make rand() return a float number between [0,1] ? (0 and 1
inclusive)

Paulo Dias

## Re:Random float number...

##### Quote
Hello,

How can i make rand() return a float number between [0,1] ? (0 and 1
inclusive)
Float RandFloat_0to1_inclusive = (float) rand() / RAND_MAX;
RAND_MAX (defined in stdlib.h) is the maximum value that can be
returned by the rand() function.
Depending on your needs, you might also take a look at _lrand() which
returns a 'long'. Dividing the value it returns by LRAND_MAX will
give a floating point number between 0 and 1, inclusive. Supposedly,
though, its period is 2^64 as opposed to 2^32 for rand().
-- salamander

{smallsort}