The principle of constructing random numbers in C language and the method of constructing random numbers by taking the remainder of rand()

    In the C language, the ANSIC C library provides the rand() function to generate random numbers. But in fact, rand() is not a real random number generator, that is, it can predict the order of random sequences, and generate random numbers between 0 and 99 under the default random seed, and its random sequence is {83,86 ,77,15,...}, such as the following program:

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

int main(){
    int num;
    printf("A set of random numbers:\n");
    for(int i = 0; i < 10; i++){
        num = rand()%100; //Generate random numbers between 0-99;
        printf("%d ",num);
    }
    putchar('\n');
    return 0;
}

    Compilation and execution results:


It can be seen that the random sequence generated by rand() is the same     when executed three times . This is because rand() uses the "magic formula" to generate pseudo-random numbers, and its random number seed has not changed, so the random sequence generated every time is the same. Before calling the rand() function, you can use the srand() function to set the random number seed. If the random number seed is not set, the rand() function will automatically design the random number seed to be 1 when it is called. The random seed is the same, and the random number generated each time will be the same. The rand() function is included in the header file <stdlib.h>.

    If you want to construct a function that generates a different random number sequence each time, you need to introduce a changing random seed. The most commonly used is the time variable, and the time function time() is used to obtain the system time (the return value time_t must be a numeric type). The argument to time() is the address of an object of type time_t where the time value is stored. Therefore, the null pointer 0 or NULL can be used as a parameter. At this point, the time is provided through the return value mechanism. The time() function is included in the header file <time.h>. Write a simple program to illustrate the time() function;

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

int main(){
    int num1,num2;
    printf("The return value of time:\n");
    num1 = time(0);
    num2 = time(NULL);
    printf("%d,%d\n",num1,num2);
    return 0;
}

    Compile and execute three times:


    It can be seen that the results obtained by using the null pointer 0 or NULL as parameters are consistent, but the results of each execution are different. Use the time function as a random seed:

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

int main(){
    int num;
    srand(time(0)); //Pass the time function to the random number generation function;
    printf("A set of random numbers:\n");
    for(int i = 0; i < 10; i++){
        num = rand ()% 100;
        printf("%d ",num);
    }
    putchar('\n');
    return 0;
}

    Results of the:


    After using the time function as a random seed, it can be found that the random sequence generated each time is different. at this time. Need to consider how to generate random numbers within a certain range. rand() generates pseudo-random numbers (positive integers) from 0 to RAND_MAX (32767). What if we want a random sequence between 1 and 100? Here we use the remainder % operation. A positive integer is modulo 5, and the resulting remainder must be between 0 and 4. Therefore, the remainder operation of modulo a is performed on a positive integer, and the remainder range obtained is in the range of 0~a-1. If b is added to the result of the remainder operation, the result obtained is between b~a+b-1. Therefore, if we want to get random numbers in the [M, N] interval, the functionrand() needs to take the remainder of (N-M+1) and add M.

    Write a program to generate a random sequence that outputs 100 [1,10] intervals.

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

int main(){
    int num;
    srand(time(0));
    printf("A set of random numbers:\n");
    for(int i = 0; i < 100; i++){
        num = rand ()% 10 + 1;
        printf("%d ",num);
    }
    putchar('\n');
    return 0;
}

Compile and execute twice:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687345&siteId=291194637