Rand () and srand () GetTickCount function usage

Source: Internet
Author: User
Tags random seed

Standard library <cstdlib> (included in <iostream>) provides two functions to help generate pseudo-random numbers:

function one: int rand (void);
Returns a random integer between [seed, Rand_max (0X7FFF)), starting with the seed specified in Srand (seed).

function two: void Srand (unsigned seed);
The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand ().

It can be thought that rand () will look at each time it is called:
1) If the user has previously called srand (SEED) and assigned a value to seed, it will automatically call
Srand (Seed) initializes its starting value at once.
2) If the user has not called srand (seed) before, it will automatically call Srand (1) once.

According to the 1th above we can draw:
1) If you want Rand () to produce a different value each time the program runs, you must give the seed in Srand a variable value, which must be different every time the program runs (such as the elapsed time so far).
2) Otherwise, if you specify a value for seed, RAND () will produce the same value each time the program runs, although this value will be a randomly obtained value between [seed, Rand_max (0X7FFF)).
3) if Srand (seed) is not called before the call to Rand (), the effect will be the same as calling Srand (1) and then calling Rand () (1 is also a fixed value).

To give a few examples, suppose we want to get a random integer between 0~6 (excluding 6 itself):

Example one, do not specify seed:
for (int i=0;i<10;i++) {
Ran_num=rand ()% 6;
cout<<ran_num<< "";
Output per run: 5 5 4 4 5 4 0 0 4 2

Example two, specifying seed as a value of 1:
Srand (1);
for (int i=0;i<10;i++) {
Ran_num=rand ()% 6;
cout<<ran_num<< "";
Output per run: 5 5 4 4 5 4 0 0 4 2
The result is exactly the same as in example one.

Example three, specifying seed as a value of 6:
Srand (6);
for (int i=0;i<10;i++) {
Ran_num=rand ()% 6;
cout<<ran_num<< "";
Output per run: 4 1 5 1 4 3 4 4 2 2
The random values are also between [0,6], with values different from Srand (1), but the results are the same for each run.

Example four, specifying the time (in seconds) that seed has elapsed for the current system: time_t times (0):
#include <ctime>
Srand ((unsigned) time (0));
for (int i=0;i<10;i++) {
Ran_num=rand ()% 6;
cout<<ran_num<< "";
First run-time output: 0 1 5 4 5 0 2 3 4 2
Second time: 3 2 3 0 3 5 5 2 2 3
In summary, each run will be different, because the time to start the program varies (the interval must be greater than 1 seconds?). See below).

About time_t Time (0):

time_t is defined as a long integer that returns the amount of time, in seconds, that has elapsed since January 1, 1970, 0 minutes and 0 seconds to date. For example, suppose the output:
Cout<<time (0);
The value is approximately 1169174701, approximately equal to 37 (years) multiplied by 365 (days) by 24 (hours) by 3600 (seconds) (not counted on the day of the month).

In addition, about Ran_num = rand ()% 6,

It is necessary to model the return value of rand () with 6 in order to ensure that the purpose is randomly grumble between [0,6], otherwise the return value of rand () may itself be huge.
A common formula is:
To obtain a random integer between [a, b], use (rand ()% (b-a)) + A (the resulting value will contain a does not contain a).
In case A is 0, the abbreviation is rand ()% B.

Finally, about pseudo-random floating-point numbers:

With RAND ()/double (Rand_max) you can get the floating-point number between 0~1 (note that the formula differs from the integral type, is divided, not modulo), for example:
Double ran_numf=0.0;
Srand ((unsigned) time (0));
for (int i=0;i<10;i++) {
RAN_NUMF = rand ()/(double) (Rand_max);
cout<<ran_numf<< "";
Operation results are: 0.716636, 0.457725, ... A floating-point number between 10 0~1, each with a different result.

If you want to take a larger range of random floating-point numbers, such as 1~10, you can
RAND ()/(double) (RAND_MAX) changed to Rand ()/(double) (RAND_MAX/10)
Operation results are: 7.19362, 6.45775, ... A floating-point number between 10 1~10, each with a different result.
As for the situation of 100,1000, so the analogy.

GetTickCount function

function function: GetTickCount returns (retrieve) the number of milliseconds from the operating system boot to the current elapsed (elapsed), and its return value is DWORD.

Function Prototypes:

DWORD GetTickCount (void);

The generation of random numbers requires a random seed, because the random number generated by the computer is obtained by means of a recursive method, and there must be an initial value, that is, the random seed that is usually said, if the random seed is not initialized, then the computer has an exact province of the random seed, so that the result of each recursion is exactly the same, So it is necessary to initialize random seeds every time the program runs, the method in VC is to call Srand (int), whose parameters are random seeds, but if given a constant, the resulting random sequence is exactly the same, so the system time can be used as a random seed, Because the system time can guarantee its randomness.
The calling method is Srand (GetTickCount ()), but it cannot be initialized with Srand (GetTickCount ()) Every time the rand () is called, because the computer now runs faster and when Rand () is called continuously, The time of the system has not been updated, so the resulting random seed is exactly the same for a period of time, so it is generally only a random seed initialization before a large number of random numbers are generated.

Rand () and srand () GetTickCount function usage

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.