hello everyone!
i'm writing a program where i want to choose a random double but not greater than "i" which is a specific value --lets say 60--i'm doing the following:

``double b = rand()%i+1``

and i get the error while compiling :

invalid operands of types 'int' and 'double' to binary 'operator%'

what might be the fault?

Edited by vyrte: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by vyrte

The rand() function returns an integer value. I recommend making "i" an integer and casting the result of this expression to a double. Try this:

``double b = (double)(rand() % i + 1);``

i've tried that and still returns integer values.
i dont know if that works:

``(float) rand()/i*10``

because when i want to have floats between 1 and i=60 for example that seems to
work but i dont know if its correct

Edited by vyrte: n/a

I believe this will work:

``float b = (float)rand()/(float)RAND_MAX * 60;``

The (float)rand()/(float)RAND_MAX will generate a value between 0.0 and 1.0, which when multiplied by 60 will give you a value between 0 and 60.

thanks a lot!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.