954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Float Generator between 2 values

I'm trying to create a random float generator. The float must be 2 decimal places and between the values 0.50 and 999.99.

I tried casting rand() to a float below, which didn't work. I'd still like to know the reason this didn't work.

saleAmount = ((float)rand() % 99950.00 + 50.00 ) / 100.00;


To make rand() a float with 2 decimal places, I did the following, which did work.

saleAmount = rand()*1.01;


I've also tried using fmod, however I can't figure out how to get the outcome between the 2 boundaries.

Any suggestions or helpful tips?

Thanks,

Miss Vavazoom

vavazoom
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Make a random integer between 50 and 99999, then divide it by 100.0

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

Thanks :)

vavazoom
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: