I'm trying to write a function in C++ that returns a random float/double in between 0 and 1 every time with a different value every time.
I've tried a few different orientations of srand, rand, and RAND_MAX but everytime, when I run my code, certain values that should change each time have constant values. I have one term thats x = 20 * randomnumber() but everytime it will return the same value for x. No matter how often I run the code. Here is my function.
double randomnumber()
{
srand(time(NULL))
double r1 = ((double)rand()) / RAND_MAX);
return r1;
}
What I want it to do is to generate a float between 0 and 1 so that when I multiply by another integer, I get a value in between 0 and that integer. Note: I know there is a function where I can do that explicitly between 0 and the number but the code I'm writing is better to just multiply by a random number every time.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire