Public Group

# odd thing

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

## 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 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 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;//orfloat r = rand();float d = r/RAND_MAX;// orfloat d = (float)rand()/RAND_MAX;

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015827
×