The three-day holiday is coming to an end soon. It is rare to have a long nap in the afternoon, so the evening is better!
I flipped through some new features of C ++ 11 and found that the random number has a new usage method and is more useful.
I have written a blog about getting random numbers with the rand function. Link: http://blog.csdn.net/start530/article/details/18713217
However, the rand function has some problems: even if not the majority, many programs require random numbers in different ranges. Some require floating-point random numbers and some require uneven distribution. When programmers try to convert the range, type, or distribution of random numbers generated by rand to solve these problems, non-Randomness is often introduced (I'm welcome to refer to the original statement in C ++ primer)
Of course, do not care about the above details. I am more concerned about the new random number acquisition method is not easy to use, can you solve the problem at the root of the male... No, solve the problem to make the random number acquisition easier.
The new random number acquisition method includes two channels: a random number engine class and a random number classification class ). An engine class can generate an unsigned random number sequence, and an distribution column uses an engine class to generate random numbers of the specified type within the specified range.
Next, I will temporarily open cocos2dx and use pure C ++ to type a few lines of code. After all, this is the root cause.
1. Use the random number engine class (to generate a random number)
The random number engine class is
Default_random_engineHere, we use it to generate a set of random numbers:
// Note that the header file # include <random> default_random_engine e; // generate a random unsigned number for (int I = 0; I <10; ++ I) {cout <e () <"; // output random number}
In a simple analysis, the above Code first defines a default_random_engine object named e, and generates the next random number through e () in the for loop. To
Note that e () does not accept the parameter and returns a random unsigned integer, that is, the generated random number is greater than or equal to 0.
2. Distribution Type (specifying the random number range)
What if we use rand to generate a random number ranging from 0 to 10? The Code is as follows:
// Int rand_num = rand () % 11; // The default_random_engine Method for rand to obtain a random number ranging from 0 to 10 can also specify the random number range, that is, its good friend distribution type: uniform_int_distribution. Usage: uniform_int_distribution <unsigned> u (); // generate a random number default_random_engine e with a uniform distribution of 0 to 10 (inclusive; // generate a random unsigned number for (int I = 0; I <10; ++ I) {cout <u (e) <endl; // The range of u is the random number range generated by e}
3. initialize Random Seed
There are two ways to seed the engine,
One is the initialization seed during creation, and the other is to call seed for initialization.. The method is as follows:
Default_random_engine e1; // use the default seed default_random_engine e2 (2300); // use the given seed default_random_engine e3; e3.seed (2300 ); // e3 uses the same seed as e2.
Because random numbers are generated, I will not post the running results of the program, as you can imagine.
Well, that's it. (Although the article is short, it can be rotated)
It is said that programmers will stay up late, but I just cannot survive. Especially after graduation, this time has formed the habit of sleeping around half past eleven. This blog is almost early in the morning, and it is no wonder that I think it is not easy to write, because it is really, too sleepy!
Respect Original, reprinted please indicate Source: http://blog.csdn.net/start530/article/details/23141863