Sign in to follow this  
derek7

odd thing

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
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

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