Go Generate random numbers in c++/c (Rand,srand usage)

Source: Internet
Author: User
Tags random seed

Reference Original address: https://www.cnblogs.com/afarmer/archive/2011/05/01/2033715.html
The random number of a computer is generated by a pseudo-random number, which is a sequence of small m-polynomial, in which each small sequence has an initial value, that is, a random seed (that is, substituting a formula to produce a series of values based on an initial value), note: The period of the small m polynomial sequence is 65535, That is, the period of random numbers generated each time a random seed is used is 65535, and when you get 65,535 random numbers, they recur.
We know that the rand () function can be used to generate random numbers, but this is not a random number in the real sense, but rather a pseudo-random number, a series of numbers based on a recursive formula on a number (which we call a seed), and when the series number is large, it is normally distributed, which is equivalent to generating a random number. But this is not really the number, when the computer is properly booted, the value of this seed is fixed, so the random number is fixed, unless you destroy the system.

    1. RAND ()
      Function: random number generator; Usage: int rand (void); header file: stdlib.h
      The internal implementation of RAND () is linear with congruential, which is not really a random number, because its period is particularly long, so in a certain range can be seen as random.
      RAND () returns the range of a random number between 0~rand_max. The Rand_max range is at least between 32676 (int) and unsigned int is 65535. Each number in the 0~rand_max is the same as the probability of being selected.
      When the user does not set a random number seed, the system default random number seed is 1. So rand () produces pseudo-random numbers, and each execution is the same "? There is a problem here, do not understand (what is the meaning of each execution)? ", by running program validation, it is found that each execution program produces a series of random numbers, but the series of random numbers produced by each run are the same; to be different, you should initialize with Srand ().
    2. Srand ()
      Function: Initialize random number generator usage: void srand (unsigned int seed) header file: stdlib.h
      The Srand () is used to set the random number seed when rand () produces a random number. The parameter seed must be an integer. If each seed is set to the same value, the random values generated by rand () will be the same every time. To make the parameters seed different for each run, we can use the current clock as a seed, because each run time is definitely different.
    • 2.1. using the current clock as a random number seed
      Using the current clock, we can use the time () function inside the header file CTime or time.h
    1. Methods for generating random numbers
    • A. Provide a seed to Srand (), which is a unsigned int type;
    • B. Call rand (), which returns a random number based on the seed value provided to Srand ();
    • C. Call rand () as many times as needed to get new random numbers without interruption
    • D. Whenever possible, give Srand () a new seed to further "randomize" the output of Rand ()
    1. Test code
#include<iostream>#include<cstdlib>#include<ctime>//#define random(a,b) (rand()%(b-a+1)+a)using namespace std;int main(){ //   srand((int)time(NULL));    for(int i=0;i<10;i++)        cout<<rand()<<‘ ‘;        cout<<endl; //       for(int i=0;i<10;i++) //       cout<<random(1,100)<<‘ ‘;    return 0;}

Result: not initialized with Srand () each run is the following series of numbers

Srand () initialization

#include<iostream>#include<cstdlib>#include<ctime>//#define random(a,b) (rand()%(b-a+1)+a)using namespace std;int main(){    srand((int)time(NULL));    for(int i=0;i<10;i++)        cout<<rand()<<‘ ‘;        cout<<endl; //       for(int i=0;i<10;i++) //       cout<<random(1,100)<<‘ ‘;    return 0;}

Result: Each run produces a different series of numbers

    1. Generate a random number within a certain range
    • A. [A, B]: (rand ()% (b-a)) +a//Take the random number pair (B-A) to take the remainder (maximum is b-a-1), plus a, that is [a, b]
    • B. [A, b]: (rand ()% (b-a+1)) +a;
    • C. (A, B]: (rand ()% (b-a)) +a+1;
    • D. (b): (Rand ()% (b-a-1)) +a+1;
    • D. General formula: A+rand ()%n, where a is the starting value, n is the range, note that the maximum value of n is n-1
    • E. To obtain a floating-point number between 0~1, you can use RAND ()/(double) Rand_max
#include<iostream>#include<cstdlib>#include<ctime>#define random(a,b) (rand()%(b-a+1)+a)using namespace std;int main(){    srand((int)time(NULL));    for(int i=0;i<10;i++)        cout<<rand()<<‘ ‘;        cout<<endl;    for(int i=0;i<10;i++)        cout<<random(1,100)<<‘ ‘;    return 0;}

Results:

Above

Go Generate random numbers in c++/c (Rand,srand usage)

Related Article

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.