Consecutive Random Numbers?

I'm creating a program that will create the effect of a rainy, foggy night to prove to my friend that C++ is better than Java. Rendering the fog and rain isn't a problem as I'm using OpenGL and have a very helpful book on the subject. What I need help with is the positioning of the rain. The way I have things set up now, I give each Rain object a random x and z value so that they won't all fall in the same place and so i don't have to individually place them all. The problem I'm having, though, is with the rand functoin. My code is as follows:
1
2
3
4
5
6
7
8
9
10
11
Rain::Rain() : debug( "RainDebug.dat" )
{
     srand( time(NULL) );      //seed the random number generator with the time
     x = (float)(rand()%2000); //set x to a float between 0 and 2000
     x /= 1000;                //divide x by 1000 so that it's between 0 and 2
     x -= 1;                   //subtract 1 from x. Now it's between -1 and 1
     srand( rand() );          //seed the random number generator with a random number
     srand( rand() );          //same as above. I'm trying to create more randomness
     z = ((float)(rand()%1000))/1000.0f; //basically the same operation as with x, except that z is between 0 and 1
     y = 1.0f;
}


As you may have guessed, I'm working inside the default space for OpenGL where a number with an absolute value greater than 1 gets clipped.

What I'm really asking is this: is there a better way to handle random numbers in C++ than this? I've done some tests and the way things are now, all of my rain objects are in the same spot. How can I fix this?
AFAIK re-seeding the RNG wont add more "randomness". And x = (float)(rand()%2000); //set x to a float between 0 and 2000 gives 0 -1999. How is this going to prove C++ is "better" than Java?

DethRaid wrote:
I've done some tests and the way things are now, all of my rain objects are in the same spot. How can I fix this?

Seed once, early in the application preferably after user input. Or use a more efficient RNG.
Last edited on
My friend is using Java's 2D engine and a lot of math to create what appears to be 3D graphics. He's currently having a lot of trouble with the fog, so I decided to show him how much easier 3D graphics with fog are in C++ using OpenGL. The code I am showing you has nothing to do with proving C++ better, actually, save that it must work and work right, which it currently doesn't. I'd appreciate any help on the subject.
Don't seed the generator more than once in your program. It doesn't make the result any "more random". Try deleting the calls to srand() on lines 7 and 8, and move the call on line 3 to the start of your program.
I guess one could prove that if the java and c++ implement virtually the same level of functionality then one could prove the following:

Suppose both run smoothly when started on machine by self on machine.

However, when you add system stressing software to run in background while testing each implementation and notice the java graphics became much faster jittery than the c++ graphics then one can assume that c++ will give you higher performance when you really need it on a much more bigger and complicated system.

But this only views performance as a measurement.
I stopped calling srand() in the constructor for Rain and moved it to the beginning of the Main function in my program and now the rain works much better. Thank you!
Maybe to make things even more equal - you guys should both implement the exact same random algorithm
Even better - get a total newbie at both languages to come up with one for you - usually a newbie always gets unexpected results quite easily :)
Although I am not following your approach for proving C++ superiority over Java (I don't think the point is there anyway) I think you can just use different variable inside your code, say double temp, calculate the number and then when temp has a value between -1 and 1 pass it to your variable of openGL.

Also for more randomness consider using a little modified approach of just using rand():
rand() does not work well in some cases (switching between 0 and 1, returning large amount of random numbers etc)

// return a random integer in the range [0, n)
int nrand(int n)
{
const int bucket_size = RAND_MAX / n;
int r;
do r = rand() / bucket_size;
while (r >= n);
return r;
}

RAND_MAX is declared in <cstdlib> and is the biggest random number that can be returned.
The approach above (mainly from Accelerated C++) just discards random values until one appropriate occur. Appropriate is one that is inside the range where all possible random numbers are equally possible to be called.

Maybe an example would clarify this:
if RAND_MAX is 10000 (normally it's much bigger)
and we want 4000 random numbers (a bit extreme case but it will give us the figure)
then by just applying rand()%4000 we get
for the first 2000 numbers 1.5 time more change of getting them than the second 2000 numbers:
1-4000 return all numbers, 4001-8000 return all numbers again and 8001-10000 returns just the first 2000 numbers. So you got the point.

Hope that helped
Topic archived. No new replies allowed.