Implementation of the lottery program

rand pseudo random number 
srand seed
clock

rand()%100; Generate random numbers from 0-99. Advanced points, if you want to generate a number between 16-59, you can write: rand()%44+16 (here 44 is obtained from 59-16+1). Do the same for other situations!


Random 50-55

first number: 55-50+1=6
rand()%6+50

Random numbers in most programs and languages ​​are indeed just pseudo-random. is a pseudorandom number generated by a determinable function through a seed. This means: if you know the seed, or the random number that has been generated, it is possible to obtain information (predictability) of the next random number sequence.

Intuitively, a computer is a determinable and predictable device, and it is obviously impossible to generate true randomness by deterministic code line by line. However, we may be able to detour...

The implementation method is simply a combination of software and hardware, or, in other words, introducing variables outside the system (think of software, code, and algorithms as a closed system).

A typical example is the random number generator (/dev/random) in the UNIX kernel, which can theoretically generate true randomness. That is, the generation of this random number is independent of the generating function, and then we say that the generator is non-deterministic.

Specifically, UNIX maintains an entropy pool that continuously collects non-deterministic device events, the hardware noise generated in the environment in which the machine is running, as seeds.

For example: the clock, the response time of IO requests, the time interval of specific hardware interrupts, the speed of keyboard strokes, the change of mouse position, and even the surrounding electromagnetic waves, etc. Intuitively, every time you press the keyboard, move the mouse, neighbors Changes in the strength of the home wifi signal, disk write speed, etc., may all be used to generate random numbers.


Therefore, the conclusion is that programs and algorithms cannot generate true randomness by themselves, but computer systems as a whole can detour to generate true randomness in the statistical sense.
 
 

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main(){
int l_v1;
for(size_t i=0;i<10;i++)
{
l_v1=rand();
printf ("l_v1 is%d\n",l_v1);
}
system("pause");
}
//My own encapsulated function
int goes to timestamp() {
time_t l_now;
time(&l_now);
return(int)l_now;
}
void set random number seed(int p_v1){
srand(p_v1);
}
int get random seed(int p_min, int p_max){
return rand() % (p_max-p_min +1)+p_min;
}

 

 

 

 

Guess you like

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