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?


Miss Vavazoom

Recommended Answers

All 2 Replies

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

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.