How to generate random numbers in C language/c++

Source: Internet
Author: User

How to generate random numbers in C language/c++

Font: [Increase decrease] Type: Reprint time: 2013-10-14 I want to comment

The rand () function, the Srand () function, and the time () function are used here. It should be stated that there is a definition of the Srand function in the iostream header file, no additional stdlib.h is required, and the use of the time () function to introduce the CTime header file

C language/c++ How to generate random numbers: the rand () function, the Srand () function, and the time () function are used here.

It should be stated that the iostream header file has the definition of the Srand function, no additional stdlib.h is required, and the use of the time () function requires the introduction of the CTime header file.

Use the rand () function to get a random number
If you just generate a random number without setting a range, you can just use RAND (): rand () returns a random value between 0 and Rand_max. Rand_max is defined in stdlib.h with a value of 2147483647.

Example 1.1:

Copy CodeThe code is as follows:
#include <iostream>
using namespace Std;
int main ()
{
for (int i=0;i<10;i++)
{
Cout<<rand () <<endl;
}
}
Results:

Use the rand () function to get a random number within a certain range

If you want to get the number within a certain range, do the corresponding division to take more than.

Example 2.1:

Copy CodeThe code is as follows:
#include <iostream>
using namespace Std;
int main ()
{
for (int i=0;i<10;i++)
{
Generates an integer within 10
Cout<<rand ()%10<<endl;
}
}
Results:

All we get is random integers, so how do we get decimals? For example: We can get an integer within 10001 (0~10000), and then divide the integer by 10000 to get the decimal of two digits after the decimal point.

Example 2.2:

Copy CodeThe code is as follows:
#include <iostream>
using namespace Std;
int main ()
{
for (int i=0;i<10;i++)
{
cout<< (rand ()%10001)/10000.0<<endl;
}
}
Note that after 10000.0 there is a decimal point, which indicates that the result is a floating-point number.

Results:

Using the RAND function and the time function
We can already get random numbers above, why do we need to use the time function? We discovered through multiple runs that although the program generates 10 random numbers, the 10 random numbers are fixed, meaning they do not change over time.

This is related to the Srand () function. The Srand () is used to set the random number seed when rand () produces a random number. Before calling the rand () function to produce a random number, the random number seed (seed) must be set with Srand (), and if no random number seed is set, rand () automatically sets the random number seed to 1 when called.

The above example is because no random number seed is set, and each random number seed is automatically set to the same value of 1, which results in the same random values generated by rand ().

Srand () function definition: void srand (unsigned int seed);

You can usually use the return value of Geypid () or time (0) as the seed

If you use Time (0), add the header file #include<ctime>

Times (0) or time (NULL) returns the period of the system (from Midnight 1970.1.1), unit: Seconds

Example 3.1:

Copy CodeThe code is as follows:
#include <iostream>
#include <ctime>
using namespace Std;

void Main ()
{
int A;
A=time (0);//time (0) returns the time of the system (from Midnight 1970.1.1), unit: Seconds
cout<<a<<endl;
}


Example 3.2:
Copy CodeThe code is as follows:
#include <iostream>
#include <ctime>
using namespace Std;
int main ()
{
Srand (Time (0));
for (int i=0;i<10;i++)
{
Generates an integer within 10
Cout<<rand ()%10<<endl;
}
}
If you do this, the results will be different for each run.

If you have questions about this article, please submit to the Exchange community, the vast number of enthusiastic netizens will answer for you!! Click to enter the community

Http://www.jb51.net/article/42065.htm

How to generate random numbers in C language/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.