How to generate random Numbers in C and C++

  • 2020-04-02 01:52:55
  • OfStack

How C /C++ generates random Numbers: the rand() function, the srand() function, and the time() function are used.

To be clear, the srand function is defined in the iostream header file without the need to introduce stdlib.h; Using the time() function requires the introduction of a ctime header file.

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

Example 1.1:


#include<iostream>
using namespace std;
int main()
{
 for(int i=0;i<10;i++)
 {
  cout<<rand()<<endl;
 }
}

Results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310140834002.jpg ">

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

If you want to get a number in a certain range, just do the corresponding division to take the remainder.

Example 2.1:


#include<iostream>
using namespace std;
int main()
{
 for(int i=0;i<10;i++)
 {
  //Produces integers up to 10
  cout<<rand()%10<<endl;
 }
}

Results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310140834003.jpg ">

We get random integers up here, so how do we get decimals? For example, we can obtain an integer within 10001 (0~10000), and then divide the integer by 10000 to get the decimal to two decimal places.

Example 2.2:


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

Note that there is a decimal point after 10000.0 to indicate that the result is a floating point number.

Results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310140834004.jpg ">

Use the rand function and the time function
We can get random Numbers up here, so why use the time function? We found that although the program generated 10 random Numbers, the 10 random Numbers were fixed, that is, they did not change with time.

This is related to the srand() function. Srand () is used to set the random number seed when rand() generates a random number. Before calling the rand() function to generate a random number, a seed must be set using srand(). If no seed is set, rand() automatically sets the seed to 1 when it is called.

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

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

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

If you use time(0), you want to include the header #include < ctime >

Time (0) or time(NULL) returns the system time(from midnight 1970.1.1) in seconds

Example 3.1:


#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) in seconds
 cout<<a<<endl;
}

Example 3.2:

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
 srand(time(0)); 
 for(int i=0;i<10;i++)
 {
  //Produces integers up to 10
  cout<<rand()%10<<endl;
 }
}

If you do that, you'll get a different result every time you run it.


Related articles: