Random numbers and random seeds

Source: Internet
Author: User
Tags current time rand random seed

The use of random numbers is less, so there is no in-depth understanding of its production principle. Here are two functions, rand () and Srand (), which generate a pseudo-random number, which is a random seed.


First, rand ()


RAND () can generate a random number between 0~rand_max and the return value is a 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 <; ++i)
	{
		cout<<rand () << "";
	}
	cout<<endl;
}


The results of the operation are as follows:




To run this function again, we get the following random number:




Second, Srand ()


From the two run results above, a problem has been found, and the sequence of random numbers generated by the two cycles called Rand () is the same. Yes, this is the pseudo-random number, as if there has been a 0~rand_max sequence in the system, we call RAND () when we refer to this sequence and random seed, there is no random seed set, so the random seed is 1, when the random seed X, We can calculate a random number f (x, M) based on this random seed X, where M is the pseudo-random number in this sequence. For example, when the random seed is 2 and the function is linear, the random number generated when the call to Rand () is 2*41, and the random number generated by the second call is 2*18467.


From here we find that if the random seed is fixed, then each call to Rand () can still be calculated, so the random seed here is expected to be unpredictable, we can take the current time, with the time () function to get the current times as random seeds, Then the random number is computed with the sequence and the current time, and the random seed is changed each time rand () is called, so the random number we generate is unpredictable. The code is as follows:

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

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





Two run results are different, because time is always changing.


As for the pseudo-random number and the random seed is by what kind of function to calculate the last random number, there are a lot of functions, I did not delve into this.


Iii. Controlling the range of random numbers

Although the return value of RAND () in random numbers is between 0~rand_max, we sometimes want to be able to control the range of random numbers produced, so we can get this effect by taking the remainder.


1. The random number that produces 1~10 rand ()% 11;

2. The random number that produces -25~25 rand ()%51

3. Generate a random number on [A, b] ((double) rand ()/rand_max) * (b-a) + A, where (double) rand ()/rand_max) can get a random number of 0~1


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

void Main ()
{
	srand ((int) time (0));
	for (int i = 0; i < ++i)
	{
		cout<< (double) rand ()/rand_max) * (60-50) + 50<< "   ";  Get the random number between 50~60
	}
	cout<<endl;
}

The results of the operation are as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.