Random numbers in C + +

Source: Internet
Author: User
Tags random seed

The random function is not an ANSI C standard and cannot be compiled under a compiler such as GCC,VC.     You can use the RAND function under C + + to implement it. 1. The C + + standard function library provides a random number generator RAND, which returns a pseudo-random integer evenly distributed between 0-rand_max. The Rand_max must be at least 32767. The rand () function does not accept parameters and defaults to 1 as the seed (that is, the starting value). The random number generator always starts with the same seed, so the pseudo-random sequence is the same and loses its random meaning. (But this is easy for the program to debug)
2, another function Srand () in C + +, you can specify a different number (unsigned integer variable) to seed. But if the seed is the same, the pseudo-random sequence is the same. One way is to let the user enter the seed, but still not ideal.
3, the ideal is to use the number of changes, such as time to be a random number generator seed. The value of time is different every moment. So the seed is different, so the random number produced is different.
C + + random function (VC program)
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace Std;
#define MAX 100
int main (int argc, char* argv[])
{srand (unsigned) time (NULL)); the//srand () function produces a random seed that starts at the current moment. It should be in front of the for-waiting loop, or it'll take a long time to wait.
for (int i=0;i<10;i++)
Cout<<rand ()%max<<endl;//max is the maximum value, and its random field is 0~max-1
return 0;
}
Ii. usage of rand ()
RAND () does not require arguments, it returns an arbitrary integer from 0 to the maximum random number, and the maximum random number size is usually a fixed large integer. This way, if you want to produce a 0~10 of 10 integers, you can express it as:
int N = rand ()% 11;
Thus, the value of n is a random number of 0~10, and if it is to produce 1~10, this is the case:
int N = 1 + rand ()% 10;
In summary, it can be expressed as:
A + rand ()% n
Where a is the starting value and N is the range of integers. A + rand ()% (b-a+1) represents a random number between a~b to 0~1 decimal, you can get the 0~10 integer, and then divide by 10 to obtain a random decimal to a very bit of 10 random decimals, you need to get a random decimal to the percentile of the first 0~ 100 of 10 integers, then all divided by 100, and so on.
Usually rand () generates random numbers that are the same as the last time they were run, and this is intentionally designed to facilitate debugging of the program. To produce a different random number each time, you can use the Srand (seed) function to randomize, and with different seed, you can produce different random numbers.
As you may say, you can also include the time.h header file, and then use Srand (Time (0)) to randomize the random number generator using the current times, which guarantees that a different sequence of random numbers can be obtained every two runs (as long as the two runs are more than 1 seconds apart).

Random numbers 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.