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?
thanks in advance

Recommended Answers

All 4 Replies

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

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!

Be a part of the DaniWeb community

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