Generate a real random number!

Source: Internet
Author: User
Tags generator

This involves the concept of pseudo random numbers, and what is pseudo random number. Pseudo-random numbers are obtained using some sort of algorithm called seed (seed) initial values. This algorithm is determined so that the resulting sequence of numbers is not statistically random. However, if the algorithm is good, then the resulting numerical sequence can pass many reasonable randomness tests. These numbers are often referred to as pseudo random numbers (psreudorandom number). Unless you know the algorithm and the seed, it is not possible to infer this sequence [1].

OK, let's take a look at what this sentence means:
Srand (Time (NULL));
Let's see what the Srand () function does. It is used to set seeds (seed), for example: Srand (n), then n is the seed you set. If you set the same seed each time, such as Srand (1), it is tantamount to not calling the Srand () function (Note: If you do not explicitly call the Srand () function, the system will invoke only one srand (1) function by default, that is, before the first rand () function, and 2nd 3rd ... RAND () does not call Srand (1) before, then you call Rand () and the sequence of random numbers is the same, here's an example:

Windows console

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void Main (void)
{
int i;

/* Seed the Random-number generator with
* The numbers is different every time we run.
*/
Srand ((unsigned) time (NULL));

/* Display ten numbers. */
for (i = 0; I < 10;i++)
printf ("%6d/n", Rand ());
}

Notice that in this program, I srand (unsigned) time (NULL) to the annotation, so each time you run the program, the seed is the default given, are the same value, (VC + + default seed value of 1), since the seed, as I mentioned earlier, the algorithm is certain , so the sequence of random numbers produced is the same every time. Every time I run a program on my computer, it generates these 10 numbers:

41

18467

6334

26500

19169

15724

11478

29358

26962

24464

So these numbers are pseudo random numbers. Because true random numbers should not be the same every time they occur, including this sequence should not be the same. So if we're going to produce real random numbers, we should set different seeds before each call to Rand, and how do we do that? Use Time (NULL).

Time (NULL) returns a time_t type of data, is actually a unsigned integer (unsigned int) of data, in this 32-bit unsigned number, high 16 bits hold the current date (month and day) information, low 16 bits of the current put time (seconds) of information. Time is changing, so the value returned each time (NULL) is different (of course two calls are more than 1 seconds long, because the information in the time_t is only accurate to seconds)
Such Srand (Time (NULL));    A new seed is guaranteed to be set each time, and then Rand () is given a different sequence of random numbers: another example [2] is simply to add a srand to the preceding example (NULL); )

Windows console

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void Main (void)
{
int i;

/* Seed the Random-number generator with
* The numbers is different every time we run.
*/
Srand (Time (NULL));

/* Display ten numbers. */
for (i = 0; I < 10;i++)
printf ("%6d/n", Rand ());
}

This will result in 10 different numbers each time the program is run, and each of these 10 numbers will be different (be aware of what I mean, the sequence of 10 numbers produced each time is different).

Note that the effect of time (NULL) is actually to save the current period to a unit and return it,

time_t Osbinarytime=time (NULL), equivalent to the following code:

CTime T=ctime::getcurrenttime ();

time_t Osbinarytime=t.gettime ();

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.