Jump to content
  • Advertisement
Sign in to follow this  
derek7

odd thing

This topic is 4884 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

these not work correctly. float d = rand()/RAND_MAX; but int r = rand(); float d = r/RAND_MAX; these work but float r = rand(); float d = r/RAND_MAX; float d =rand()/0x7fff; why where is the problem?

Share this post


Link to post
Share on other sites
Advertisement
I am not for sure but i think it is because you are declaring float d two times.

float d = rand()/RAND_MAX;

but int r = rand();
float d = r/RAND_MAX;

I am not well up with rand() but there you have float d = rand()/RAND_MAX and a second that has the same name so the compiler won't know which is which. You need to rename the other float so that it will work [smile].

Edit: also why would you want to assigne two randum number generators together?

Share this post


Link to post
Share on other sites
That's two different snippets (both doing the same thing, but the second is spread out).

I believe that it's not working because your number is getting rounded (since it has an int in it). If you convert to a float or use a float to begin with, it should work fine:

int r = rand();
float d = (float)r/RAND_MAX;

//or
float r = rand();
float d = r/RAND_MAX;

// or
float d = (float)rand()/RAND_MAX;


Share this post


Link to post
Share on other sites
rand()/RAND_MAX

Both terms are integers, thus the compiler generates an integer division. Given that RAND_MAX is always larger than rand(), the result is always integer zero, which then gets converted to a float. You need to explicitely convert one of the terms to a floating-point value to cause a floating-point division to be generated.

rand() / (float)RAND_MAX

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!