How to produce random number _c language in C language/c++

Source: Internet
Author: User
Tags function definition rand

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

It should be explained that there is a definition of the Srand function in the iostream header file, no additional stdlib.h is required, and the time () function requires the introduction of the CTime header file.

Use the rand () function to get a random number
If you just generate random numbers and don't need to set a range, you can just use RAND (): rand () returns a random number ranging from 0 to Rand_max. Rand_max is defined in Stdlib.h, and its value is 2147483647.

Example 1.1:

Copy Code code 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, just do the corresponding division of the remainder can be.

Example 2.1:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main ()
{
for (int i=0;i<10;i++)
{
To produce an integer within 10
Cout<<rand ()%10<<endl;
}
}

Results:

We get all of the random integers, so how do we get decimals? For example, we can get an integer (0~10000) less than 10001, and then use this integer divided by 10000 to get two decimal places.

Example 2.2:

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main ()
{
for (int i=0;i<10;i++)
{
cout<< (rand ()%10001)/10000.0<<endl;
}
}

Note that 10000.0 is followed by a decimal point that indicates the result is a floating-point number.

Results:

Using the RAND function and the time function
We can already get the random number above, why do we need to use the time function? We discovered through multiple runs that the program generates 10 random numbers, but this 10 random number is fixed, which means it doesn't change over time.

This is related to the Srand () function. Srand () is used to set the random number seed for rand () to produce random numbers. Before you can invoke the rand () function to generate random numbers, you must first use Srand () to set up the random number seed (seed), and if no random number seed is set, rand () automatically sets the random number seed to 1 when it is called.

The above example is because no random number seed is set, each random number seed is automatically set to the same value of 1, which leads to the same random values produced by Rand ().

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

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

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

Times (0) or time (NULL) returns the system (from Midnight 1970.1.1), in seconds

Example 3.1:

Copy Code code as follows:

#include <iostream>
#include <ctime>
using namespace Std;

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


Example 3.2:
Copy Code code as follows:

#include <iostream>
#include <ctime>
using namespace Std;
int main ()
{
Srand (Time (0));
for (int i=0;i<10;i++)
{
To produce an integer within 10
Cout<<rand ()%10<<endl;
}
}

By doing so, the results of each run will be different.

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.