Sign in to follow this  

Rand() - Whats wrong??

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

its 4:42 am so i apologise for any shocking spelling mistakes i might make. Anyway the snippet of code below shows the display for my asteroids environment. As i'm sure you can see i am trying to use a rand() function seeded by the computers timer. However at the moment all i am getting are the asteroids butforming a sort of quadilateral. Could anyone suggest a way in which i could remedy this and yet still get random placement of my asteroids within the world. p.S - i'm new please be gentle void display (void) { float i; srand (timeGetTime()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; glRotated(roll, 0, 0, 1); glRotated(pitch, 1, 0, 0); glRotated(yaw, 0, 1, 0); glTranslated(-Px, -Py, -Pz); glBindTexture(GL_TEXTURE_2D, texture[0]); // draw_square (1.0); // glCallList(asteroid1); for (i=0; i <=5; i++){ glLoadIdentity(); rx = rand()%(2); ry = rand()%(2); rz = rand()%(2); glRotated(roll, 0, 0, 1); glRotated(pitch, 1, 0, 0); glRotated(yaw, 0, 1, 0); glTranslated(-Px, -Py, -Pz); glTranslatef(rx,ry,rz); glCallList(asteroid2); } glutSwapBuffers(); } Cheers in advance for any help given

Share this post


Link to post
Share on other sites
Sorry the above isn't a very good explanation. Basically i am using
ry = rand()%(2);

because if i put in rand()%(1); nothing happens. Or it does but i just get5 asteroids in 1 place.

I think what i really need to apply is a millisecond count or something but i have absolutely no idea how to do that and can't find it anywhere.

Once again cheers for any help that may be given.

Neil

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
rand() returns a number between 0 and RAND_MAX (or INT_MAX, whatever). Then you're taking that number, modulus 2: so it's giving you either 0 or 1. rand() % 1 is always zero, because zero divides every integer, which is why that makes all your things show up in the same place.

Share this post


Link to post
Share on other sites
One method also, though maybe kind of slow is to divide the rand() by RAND_MAX, this will get you a number between 0 and 1. Then multiply this by the max number you want to get and you have between 0 and your max like this.
float random(float min, float max)
{
return(((float)rand()/RAND_MAX)*(max-min)+min);
}
this gets the rand value, divides it by randmax to go between 0 and 1. Then multiplies it by the difference between max and min, finally adding min. For example, you want between 2 and 5. after dividing by RAND_MAX, you get 0.5, you then multiply by the difference between max and min, getting 1.5, then adding min, you get 3.5, which is between 2 and 5.
This number works great for positioning objects, like asteroids, I have used it before.

Share this post


Link to post
Share on other sites
Cheers guys - in the code above i am now having a new problem.

Where i have got the keyboard controls - i.e the roll pitch and yaw settings - i am now having trouble keeping each one on a predictable path.

Instead of rotating around the origin of the world the just seem to a new randomly generated place. How can i control this so they rotate around the origin?

Share this post


Link to post
Share on other sites
Use code like this:

<code>
glPushMatrix();
glLoadIdentity();
glRotated(yaw,0,1,0);
glRotated(pitch,1,0,0);
//Draw stuff here
glPopMatrix();
</code>

You may need to negate yaw and/or pitch, depending on how they are stored.

Share this post


Link to post
Share on other sites

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this