• Advertisement

Archived

This topic is now archived and is closed to further replies.

Randomizing Floats?

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

How would you go about randomizing floats? So you could say get back between 0.0 - 1.0 any number (within a certain degree) between the two?

Share this post


Link to post
Share on other sites
Advertisement
float result = (float)rand()/(float)RAND_MAX;

rand() returns a number between 0 and RAND_MAX, so this should give you a 0.0f to 1.0f range.

Share this post


Link to post
Share on other sites
Or use some code like the Mersenne Twister that will give you 0.0f to 0.9999999999f and do it faster than the rand statement can give you an integer.

Share this post


Link to post
Share on other sites

  • Advertisement