This morning I was about to repost a blog that looked good and was recommended by cocos2dx official Weibo. Who knows that the blog has become private when the link is opened in the morning. I knew that I should have copied the content last night. It's not amazing to have an original article with a thick face today. Haha.
1. Simple Random Number usage: Example of CCRANDOM_0_1:
Int HelloWorld: getRand (int start, int end) {float I = CCRANDOM_0_1 () * (end-start + 1) + start; // generate a random number return (int) I from start to end ;}
2,The preceding method is simple, but after running multiple times, the random numbers are all the same. Why? This involves the Random Seed..
What is a random number seed? Here we may wish to... Baidu.
There is no real random number generator in the computer, but the generated digital repetition rate can be very low. This looks like a real random number. The program that implements this function is called a pseudo random number generator.
There are many theories about how to generate random numbers. To discuss them in detail, we need a thick book. No matter what method is used to implement the random number generator, an initial value named "Seed" must be provided to it. And the value should be random, or at least the value is pseudo-random. The value of the "seed" is usually generated using the fast count register or shift register.
Now, after reading the above introduction, we have some concepts about the random number seed. Next, let's talk about how to initialize the random number seed when calling the random number. The Code is as follows:
cc_timeval psv; CCTime::gettimeofdayCocos2d(&psv, NULL); unsigned long int rand_seed = psv.tv_sec*1000 + psv.tv_usec/1000; srand(rand_seed);
Don't ask me why it is written like this, just like "don't ask where I come from. The usage is as follows:
cc_timeval psv; CCTime::gettimeofdayCocos2d(&psv, NULL); unsigned long int rand_seed = psv.tv_sec*1000 + psv.tv_usec/1000; srand(rand_seed); For(int i=0;i<100;i++){ int _rand = getRand(1,100); CCLOG(“the _rand is : %d”,_rand);}
Well, I don't know if you have any questions after reading it. Why don't we initialize the random number seed for every for loop? Isn't it even more random? For example:
for(int i=0;i<100;i++){cc_timeval psv; CCTime::gettimeofdayCocos2d(&psv, NULL); unsigned long int rand_seed = psv.tv_sec*1000 + psv.tv_usec/1000; srand(rand_seed); int _rand = getRand(1,100);CCLOG(“the _rand is : %d”,_rand);}
After running, you will find that the generated number is not random. The original random number seed initialization must be out of the loop, so there is no effect in the loop. If you want to ask me why this is the case, I can only say "Don't ask me from ....".
Haha, you're kidding me. Let me give myself a guess:
The random number seed is equivalent to the random number reset switch. If you want to obtain a random number, you must first turn on the switch and initialize the random number seed, which is equivalent to turning on the switch, at this time, the "Seed" starts to get up. When you get a random number, it will give you a different location data, if you initialize the Random Seed (that is, the restart switch) for every time you get the data, it is equivalent to the "Seed" and re-starting from the starting point. Isn't this a exhausting "Seed" cycle?
Haha, it's all nonsense. Let's take a look.
3. Next we will explain how to generate a random number of K in a certain range.
I can find several implementation methods on the Internet. Here I only record the practices that are considered more efficient.
Use array A [] to store the values of x to y, then generate the first random number H in (x, y) as the subscript, and retrieve A [H] From array A, then assign the last element of the array to A [H] And then generate it again in (x, Y-1), such as loops.
Code Implementation
Int quantity = 12; int start = 0; int end = 36; int total = abs (end-start); if (quantity> total) {CCLog ("random number error");} int sequence [total]; // array where random numbers are stored int output [quantity]; // The final generation does not repeat a series of random numbers // Initialize sequence for (int I = 0; I <total; I ++) {sequence [I] = start + I ;} // Random seed cc_timeval psv; CCTime: gettimeofdayCocos2d (& psv, NULL); unsigned long int seed = psv. TV _sec * 1000 + psv. TV _usec/1000; srand (seed); for (int I = 0; I <quantity; I ++) {int num = this-> random (0, end-1 ); // generate a random number in the specified range output [I] = sequence [num]; // store the generated random number sequence [num] = sequence [end-1]; // fill the value of the last submark in the randomly generated subscript end --; // move forward in the specified range}
Well, write this. Ha
Forward please note the Source: http://blog.csdn.net/start530/article/details/18713217