PDA

View Full Version : rand() help



08-23-2001, 09:20 AM
ok I am doing little random coloring on c++/opengl...
But I got little problem when I use rand()...
so I made little program...
But after compile and running it several times I always get same numbers...
I know this is not opengl problem but this is the best coding forum...

#include <iostream.h>


int a,b,c;
float d,e,f;

void main(void)
{
f=3.2f;
a=(int)rand();
b=5+rand()%6;
c=(int)rand();
d=(float)rand()/3;
e=4*rand()%3;
//f=rand();
cout << rand() <<endl;
cout << b <<endl;
cout << c <<endl;
cout << d <<endl;
cout << e <<endl;
cout << f <<endl;
}

rts
08-23-2001, 09:36 AM
You may want to look up the documentation for the function "srand".

Thug
08-23-2001, 09:55 AM
You may also want to look in to calling randomize() then try using random(...)

randomize will create a random seed to start everytime the program executes.

Mezz
08-23-2001, 10:00 AM
srand() is a function that will 'seed' the random number generator to ensure it generates different numbers each execution.

You only need to call it once in your initialisation code, most people usually use the time as a seed (then it varies indefinately, or until time ends if you believe in that).

-Mezz

no-one
08-23-2001, 05:39 PM
no that they've fixed that problem try a faster generator thats far more random...

4x faster that rand() and its better...
http://www.math.keio.ac.jp/~matumoto/emt.html