-
August 4th, 2005, 10:26 PM
#1
[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.
-
August 4th, 2005, 11:22 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;
}
-
August 5th, 2005, 01:37 AM
#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().
-
August 5th, 2005, 07:51 AM
#4
Re: how do i generate a random float between 0 and 1??
To get a better random number generator search for "Mersenne twister".
-
August 5th, 2005, 09:10 AM
#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;
}
"Programs must be written for people to read, and only incidentally for machines to execute."
-
August 5th, 2005, 09:16 AM
#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.
-
August 5th, 2005, 09:27 AM
#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.
-
August 5th, 2005, 01:26 PM
#8
Re: how do i generate a random float between 0 and 1??
Hungarian notation is the bane of self documenting code.
Forget all fancy tricks with operator precedence. Code should be easily readable.
May the BOOST be with you.
Good free E-Books thanks to Bruce Eckel.
-
August 6th, 2005, 09:13 PM
#9
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.
-
August 8th, 2005, 12:57 AM
#10
Re: how do i generate a random float between 0 and 1??
Kevin Hall
-
September 23rd, 2007, 02:54 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|