C + + series: Random number

Source: Internet
Author: User

There is no self-contained random function in C + +, and rand () and Srand () are required to generate a random number. However, since the internal implementation of RAND () is made with linear congruential, it is not a real random number, but a random pseudo-random number that can be seen within a certain range.

Rand
Srand
Formula
Rand
The Pure rand () returns a random number between 0 and Rand_max, whereas the value of Rand_max is related to the number of int digits, and the minimum is 32767. Rand () is a one-off, however, because the default random number seed of the system is 1, so long as the random number seed remains unchanged, the sequence of random numbers generated will not change.

In fact, for Rand (), we can be artificially set, we just need to define a random (int x) function in the macro definition, we can generate random values ranging from 0 to X. Of course, it can also be defined as random (a, a, a, b) so that it is generated in a range of numbers from a to a. The concrete definition method is in the General formula section.

Srand
Srand () can be used to set the random number seed when rand () produces a random number. By setting different seeds, we can get different sequences of random numbers. You can use the Srand ((unsigned int) (Time (NULL)) method to produce different random number seeds using the system clock. However, to call time (), you need to add the header file < CTime >.

Examples are as follows:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace Std;
int main ()
{
Srand ((unsigned) time (NULL));
for (int i=0;i<10;i++)
Cout<<rand () << ";
return 0;
}
Formula
The general expression formula for generating a certain range of random numbers is:

Gets the random Integer (0,x): rand ()%x;
Obtain a random integer (A, B): rand ()% (b-a);
Obtain a random integer [A, b]: rand ()% (b-a) +a;
Obtain a random integer [A, b]: rand ()% (b-a+1) +a;
Obtain a random integer (A, b]: rand ()% (b-a) +a+1;
Gets the floating point number between 0-1: Rand ()/double (Rand_max).
Examples are as follows:

#include <iostream>
#include <cstdlib>
#include <ctime>
#define Random (A, B) (rand ()% (b-a+1) +a)
using namespace Std;
int main ()
{
Srand ((unsigned) time (NULL));
for (int i=0;i<10;i++)
Cout<<random (1,100) << ";
return 0;
}

C + + series: Random number

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.