CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: how do i generate a random float between 0 and 1??

1. Member
Join Date
Jun 2005
Posts
94

## [FIXED] how do i generate a random float between 0 and 1??

Hi, i need to generate a random float number between 0.0 and 1.0 for use in my OpenGL program but i can't seem to get it to work.

i tried
float r = (float) rand () / (GLfloat) RAND_MAX;
but doesn't work coz my red colour ends up being 0.
Last edited by thre3dee; August 4th, 2005 at 10:32 PM.

2. ## Re: how do i generate a random float between 0 and 1??

Code:
```#include <time.h>
#include <iostream>

using std::cout;
using std::endl;

int main()
{
srand( (unsigned)time( NULL ) );

for (int i = 0; i < 5; i++)
{
cout << (float) rand()/RAND_MAX << endl;
}

return 0;
}```

3. ## Re: how do i generate a random float between 0 and 1??

RAND_MAX is often 32767. If you want to make use of the full 23 bits of mantissa then:
Code:
```const unsigned long RAND_MAX_PLUS_1 = RAND_MAX + 1;

unsigned long bigRand()
{
return rand() * (RAND_MAX_PLUS_1) + rand();
}```
the new RAND_MAX will be (RAND_MAX+1)²-1
Code:
```const unsigned long BIG_RAND_MAX = RAND_MAX_PLUS_1 * RAND_MAX_PLUS_1 - 1;
float rand0to1()
{
return static_cast<float>(bigRand() ) / BIG_RAND_MAX;
}```
(You can also make BIG_RAND_MAX a float or a double instead of an unsigned long).
Note if you want to make use of the precision of double, you could require 4 calls to rand().

4. ## Re: how do i generate a random float between 0 and 1??

To get a better random number generator search for "Mersenne twister".

5. ## Re: how do i generate a random float between 0 and 1??

It's not good to rely on certain value of RAND_MAX constant. It's better to do smth like this:
Code:
```float random()
{
float scale=RAND_MAX+1.;
float base=rand()/scale;
float fine=rand()/scale;
return base+fine/scale;
}```

6. ## Re: how do i generate a random float between 0 and 1??

Yes it is better to use RAND_MAX as a float or double because my unsigned long may well overflow.

The compiler may be able to optimise your division by scale but I wouldn't count on it. If you make scale a const float it is more likely to.

I don't know if you can assume that RAND_MAX will always be (2^N) - 1 for some N.

7. ## Re: how do i generate a random float between 0 and 1??

With all this playing with rand_max etc you are not sure if the resulting random numbers will be truly (pseudo-)random. Therefore, I suggest to use a Mersenne twister which can generate a floating point random number in 0-1.

9. Member
Join Date
Jun 2005
Posts
94

## Re: how do i generate a random float between 0 and 1??

no i fixed it i was setting it to a local float not the class member float so the class member waasn't actually getting the value.

10. Senior Member
Join Date
Nov 2002
Location
Foggy California
Posts
1,245

## Re: how do i generate a random float between 0 and 1??

11. Junior Member
Join Date
Sep 2007
Posts
1

## My humble solution

My humble solution

float rand_FloatRange(float a, float b)
{
return ((b-a)*((float)rand()/RAND_MAX))+a;
}

Work fine with any range. All I did was to treat rand() as % and multiply that result by range between 'a' and 'b'

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•