Hello,
I want to have a random number generator.
Currently I'm using
fdur = (rand()%2)+0.7;
But I've been having some inconsistencies so perhaps I'm doing something wrong.
The "fdur" variable is a float.
A fast, easy to use, free, and community supported 2D game engine
Hello,
I want to have a random number generator.
Currently I'm using
fdur = (rand()%2)+0.7;
But I've been having some inconsistencies so perhaps I'm doing something wrong.
The "fdur" variable is a float.
rand()%2 will be either 0 or 1, so fdur will always be either 0.7 or 1.7. Is that what you're after?
No. I was thinking of a range
From 0.7 to 2 seconds. Of course going through 0.8, 0.9 , 1, 1.1 etc etc
How can I get that?
@fms,
Unless you have some some sort of extraordinary entropy requirement, you could just do it the easy way:
float theMinimum = 0.7f;
float theMaximum = 2.0f;
float theRandomRange = theMaximum - theMinimum;
float theResult = (CC_RANDOM_0_1() * theRandomRange) + theMinimum;
That will give you a number between theMinimum and theMaximum .
See CC_RANDOM_0_1() in the source.
Mark
Thank you.
I have a question what is CC_RANDOM_0_1() ??
Is that some kind of random generator?
Put it in your code and then command-click on it to see the definition.
Oh thanks I just did that. It really works for me. It actually removed the strange change of time the third and fourth time I returned from menu to game scene.
Make sure you seed the random function somewhere when the app delegate is initialized - like using the current time for the seed usually works well : srandom(time(0)
You must log in to post.