C language to generate random numbers [simple card draw code as an example]

Article directory

  • foreword
  • 1. Generate random numbers
    • rand() function
    • srand() function
    • time() function
    • generate a truly random number
  • 2. Use tips
  • 3. Use code example (simple card draw)
  • Summarize

Foreword

This article will explain in detail how to generate random numbers in C language, and introduce the application tips

1. Generate random numbers

C language needs to use three functions to generate a true random number: rand(), srand() and time().

Introduction to functions

rand() function

Header file: #include
Function declaration: int rand(void)
Function; Generate a sequence of pseudo-random numbers, the range of random numbers is 0~RAND_MAX

PS – RAND_MAX is actually a macro name, and there is a macro definition in the header file
#define RAND_MAX 32767
Different compilers will define it as a different value, the compiler used in this article defines it as 32767

Function logic: In the function declaration, the function call symbol () is void, so no parameters are required, and the function rand() will return a pseudo-random number rand() function and srand() function It must be used together. The rand() function cannot generate random numbers by itself. The seed (starting point) must be set in the srand() function to return the random number. The rand() function will check whether srand() is defined before calling, if not, it will automatically generate srand(1) and call it

srand() function

Header file: #include
Function declaration: void srand (unsigned int seed);
Function: It makes sense to use it together with the rand() function. Only when the srand() function is set such as srand(1) and srand(2), rand() can will return a sequence of pseudo-random numbers

The rand() function and the srand() function must be used together. The srand() function is the seed planted, and the rand() function is the fruit it bears. Without the seed (srand), there is no fruit (rand)

Function logic: srand(): Pseudo-random number generator. The function call symbol () of the function declaration is unsigned int seed (seed is translated as seed in Chinese), so we need to pass an unsigned integer as seed (seed) to generate a random number sequence. But for each seed such as srand(1), the generated random number sequence is fixed, so the srand() function is called a pseudo-random number generator

pseudorandom number sequence
What kind of seed will bear what kind of fruit, every time a seed is defined, the compiler will generate a fixed sequence of pseudo-random numbers
Below I use the seed srand(1) to generate 10 pseudo-random numbers

#include <stdio.h>
#include <stdlib.h>

int main()
{<!-- -->
srand(1);
for (int i = 1;i<=10; i + + )//ten pseudo-random numbers
{<!-- -->
printf("%d\\
", rand());
}
\t
}

Run the code

The ten fixed numbers have been generated by the compiler. The compiler I use is **visual studio 2019**, that is to say, no matter when and where I am, on someone else's computer or on my own computer, as long as The compiler is **visual studio 2019**, and the first ten pseudo-random numbers generated by the seed srand(1) are these ten numbers.

The pseudo-random number sequences generated by different compiler seeds are different. I am here in the visual studio 2019 environment

So how can we get real random numbers? In fact, the problem is very simple, we only need to put a changing number in srand() to generate a truly random number sequence,
So what are the numbers that keep changing in reality? That’s right, it is time, and the function that generates time in C is the time() function

time() function

Header file: #include
Function declaration: time_t time (time_t* timer);

typedef _int64 time_t exists in the header file
Among them, _int64 is actually long long, so time_t represents long long integer

Function: The time() function is used to obtain the time stamp (time stamp) of the current system time, that is, the elapsed time from January 1, 1970 00:00:00 UTC to the current time seconds
Function logic: The function call symbol () of the function declaration of the time() function is time_t* timer, here is to obtain the timestamp through the pointer, but we use more time(NULL ) In this way, time (NULL) does not need to create a pointer, and directly returns the current time.

Generate a true random number

We can set the srand like this
srand((unsigned)time(NULL));
At this time, the seed parameter in the srand() function is constantly changing, and the generated random number is also constantly changing

The time() function is of time_t type (long long type), but the seed parameter of the srand function () needs to be of unsigned int type, so it is best to use: () to force type conversion for conversion

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{<!-- -->
\t
\t
srand((unsigned)time(NULL));//time as seed
for (int i = 1; i <= 10; i ++ )
{<!-- -->
printf("-:%d\\
", i, rand());
}

}

Run the code

If you copy this code now, the ten numbers generated must be different from the numbers I generated above

2. Use tips

Use% to limit the generated random numbers to a certain range
Generate random numbers from 0 to 9

int a=rand() ;printf("%d",a);

Generate random numbers from 10 to 20

int a=10 + rand ;printf("%d",a)

%a, the value range is 0~a-1, by using it reasonably, you can generate random numbers that meet your requirements

3. Use code example (simple draw card)

In programming, the appearance of random numbers brings us a lot of convenience
For example, to write a rock-paper-scissors game, the computer can use rand()%3 to generate “0”, “1” and “2” to represent “rock”, “scissors” and “paper”, etc… ··

Next, I will use srand(), rand() and time() to generate 10 random numbers with values ranging from 3 to 6, and use code to simulate a ten-consecutive card draw
Simple card drawing system

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{<!-- -->
int three_star = 0;
int four_star = 0;
int five_star = 0;
int six_star = 0;
srand((unsigned)time(NULL));
for (int i = 1; i <= 10; i ++ )
{<!-- -->
int a = 3 + rand() % 4;//generate random numbers from 3 to 6
switch (a)
{<!-- -->
\t\t
case 3:three_star ++ ; break;
case 4: four_star ++ ; break;
case 5: five_star ++ ; break;
case 6:six_star ++ ; break;

}
}
printf("The results of this draw are as follows\\
");
printf("Samsung operator: %d name\\
", three_star);
printf("Four-star operator: %d name\\
", four_star);
printf("Five-star operator: %d name\\
", five_star);
printf("Six-star operator: %d name\\
", six_star);

return 0;

}

Run the code

Hahaha, the luck is not bad, a six-star shipment, if readers are interested, you can copy this code to test your luck, and share your shipment status in the comment area

Summary

This article introduces its basic principles, here is just the tip of the iceberg of these functions, skilled programmers will use them superbly
It is not easy to sort out, if it is helpful to you, then give a triple, thank you