Random number and a random seed testing

        Less use of random numbers, so there is no understanding had produced its principles. This involves two functions, RAND () and srand (), the former is to generate a pseudo-random number, which is to generate a random seed.

A, rand ()

          rand () may generate a random number between 0 ~ RAND_MAX, the return value is an unsigned int type value. The following code:
 

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

void main()
{
	for (int i = 0; i < 10; ++i)
	{
		cout<<rand()<<" ";
	}
	cout<<endl;
}

Results are as follows:

Run this function once again, we get a random number as follows:

Two, srand ()

From the above results of the two runs, a problem was found, two cycles call rand () generated random number sequence is the same. Yes, this is a pseudo-random number, as if it already has a 0 to a scrambled sequence RAND_MAX in the system, we call rand () all the time with reference to the sequence and random seed, there is no set random seed, a seed randomly therefore, when the random seed of x, we can calculate a random number F (x, m) based on this random seed x, where m is the pseudo-random number sequence. For example, when a random number, called once rand () when random seed is 2, is a linear function of 2 * is generated on the 41, the random number generated by the second call to the 2 * 18467.

From here we found that, if the random seed is fixed, then each call to rand () can still be calculated, and therefore, the random seed of hope here can be unpredictable, we can get the current time with the time () function to get the current time as a random seed, then with the current time and calculated random number sequence, each time you call rand () when the random seed is change, so we generate a random number that is unpredictable up. code show as below:
 

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

void main()
{
	srand((int)time(0));
	for (int i = 0; i < 10; ++i)
	{
		cout<<rand()<<" ";
	}
	cout<<endl;
}

Two different operating results, because the time is always changing.

As for the pseudo-random number and a random seed is calculated by a function of what the final results of random numbers, there is a lot of functions, I do not have in-depth study of this.

 

Third, the control range of the random numbers

Although the random number rand () returns a value between 0 and RAND_MAX, but we also want to be able to sometimes look at a specific control range of the random number generated, then, we can achieve this effect by the method of modulo.

1, generates a random number rand 1 ~ 10 is (11%);

2, generates a random number rand -25 ~ 25 is (51%)

3, is generated [a, b] the random number ((double) rand () / RAND_MAX) * (ba) + a, where (double) rand () / RAND_MAX) can be a random number between 0 and 1
 

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

void main()
{
	srand((int)time(0));
	for (int i = 0; i < 10; ++i)
	{
		cout<<((double)rand()/RAND_MAX)*(60 - 50) + 50<<"   ";  //获取50~60之间的随机数
	}
	cout<<endl;
}

Guess you like

Origin blog.csdn.net/qq_30263737/article/details/93467940