The random number in C + +

Source: Internet
Author: User
Tags float number function prototype

This article is divided into two parts, first introduced in C language with random numbers related to the two functions Srand and Rand, after the introduction of the random library in C + +.


1. Srand and Rand in the C language


1) Implementation of the following is the implementation of the VC:
#define Rand_max 32767//in <stdlib.h>unsigned long _randseed = 1;//global seedvoid srand (unsigned int seed) {_rand seed = Seed;} int rand (void) {_randseed = _randseed * 1103515245 + 12345;return ((unsigned int) (_randseed >>) & Rand_max);}

The first contact in the C language of random numbers, very confused why there is a seed this thing, only to provide a function to generate a random number is not on the line, after looking at the above source, it is clear, because the computer can not produce a real random number , only by the mathematical method to produce pseudo-random number. The role of the Srand function is to set the seed, if not set the seed (above the _randseed) default is 1, the seed is a global variable. Rand's implementation is related to number theory, you can see its return value range is [0, Rand_max], the standard is the same.


2) Time

Now that the computer doesn't produce a real random number, how can you make the program run differently each time? There has to be a random thing, then the time this function to produce seeds, introduce a new thing and bring some pits, I wrote this program in the early years:

Generate 10 random numbers (Error usage) for (int i = 0; i < ++i) {Srand ((unsigned int) time (NULL));p rintf ("%d:%d\n", I, Rand ());}
It will produce 10 identical numbers. To explain the problem, you have to understand the function of time, which has the following function prototype:
time_t time (time_t *timer);

It returns the "current Time", the "time" type is time_t, in the VC is a typedef for unsigned long, the standard only specifies that it is an arithmetic type, as to how it represents the undefined time. This is typically the return UNIX timestamp, defined as the total number of seconds from 00:00 GMT, January 01, 1970, from 00 seconds to the present. The above program executes quickly and completes the loop within a second, so it produces the same random number.


3) My_rand

Here are three templates for generating random numbers

/*** return a random integer in the interval** [0, Rand_max]*/int My_rand () {static int is_first = 1;if (is_first) {Is_fir st = 0;srand ((unsigned int) time (NULL));} return rand ();} /*** return a random integer in the interval** [A, b]*/int uniform_int (int a, int b) {static int is_first = 1;if (is_first {Is_first = 0;srand ((unsigned int) time (NULL));} return (int) ((double) rand ()/((Rand_max + 1.0)/(B-a + 1.0) + a);} /*** return a random real in the interval** [A, b] (also [A, b)) */double uniform_real (double A, double b) {static int is_f Irst = 1;if (is_first) {Is_first = 0;srand ((unsigned int) time (NULL));} Return (double) rand ()/((double) Rand_max/(B-A)) + A;}
They can be called separately, when the initial simultaneous calls to Uniform_int and uniform_real, they will produce two very close to the number, if you care about this can put srand outside, only call once srand. When the requested random number range is too large, uniform_int and uniform_real seem to have bugs.


2. Random Library


There is a random number generator (engine/generator) and distribution (distribution) in the random library, and their specific usage I'm not going to say that. I personally think that the engine stores the seeds and encapsulates the global seed in the C language. Only the maximum and minimum values (two parameters of the average distribution) are stored in the uniform distribution. In fact, there is a real engine called std::random_device, which generates random numbers based on the various real-time parameters of the machine. Similarly, two templates are provided below.
/*** return a random integer in the interval [A, b]*/int uniform_int (int a, int b) {static std::d efault_random_engine e{st d::random_device{} ()}; Avoid "most vexing parse" static std::uniform_int_distribution<int> u;return u (E, Decltype (u)::p Aram_type (A, B) );} /*** return a random real in the interval [a, b] (also [A, b]) */double uniform_real (double A, double b) {static std::d Efau Lt_random_engine e{std::random_device{} ()};static std::uniform_real_distribution<double> u;return u (E, Decltype (U)::p Aram_type (A, b));}

Resources

[1] C standard library [2] C and pointer [3] random number is deceptive,. Net, Java, C testify for me
[4] How to generate a random number in C? [5] Generate random numbers uniformly over an entire range[6] Generate a random number within range? [7] C + + random float number Generation[8] C + + Primer

The random number in C + +

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.