当前位置:网站首页>Rand function generates pseudo-random numbers
Rand function generates pseudo-random numbers
2022-07-28 20:01:00 【Hello_ World_ two hundred and thirteen】
call rand() Before the function, you need to call srand function
srand The function sets the starting point for generating a series of pseudo-random integers .
To reinitialize the generator , Need to be for srand Provide a random seed parameter .
The value of the seed will set the generator to a random starting point .
We use time stamps as seeds -- srand((unsigned int)time(NULL))
rand Retrieves the generated pseudo-random number .
Show the phenomenon through the results ( To compare a series of codes ):
(1)
Once you pass the random seed ( Time stamp ) Set it up srand The starting point , Will pass through rand Generate a series of real pseudo-random numbers
Note that random seed setting is used here srand The starting point is not called !! If the concept here is vague , Skip first and look back , You can understand .
// True pseudorandom
int main()
{
srand(time(NULL));
while (1)
{
int num1 = rand();
int num2 = rand();
printf("%d %d\n", num1, num2);
}
return 0;
}

(2)
If you set it by calling multiple times srand The starting point , And each setting time is less than 1s, So here 1s Inside , rand The function generates the same value , Until the next second . Summed up in :srand Starting point every 1s Reset it again
// Error model 1
int main()
{
while (1)
{
srand(time(NULL));
int num1 = rand();
int num2 = rand();
printf("%d %d\n", num1, num2);
}
return 0;
}
(3)
Set with a fixed value as the seed srand The starting point ,rand The generated value will never change .
Error model 2:
int main()
{
while (1)
{
srand(1);
int num1 = rand();
int num2 = rand();
printf("%d %d\n", num1, num2);
}
return 0;
}
(4)
Here are the results of two runs , It's not hard to see. , A single time seems random , Each actual run rand The generated values correspond to equal The reason is that it is not used srand Set the starting point , The default starting point is seed 1 Of srand The starting point .
Error model 3:
int main()
{
while (1)
{
int num1 = rand();
int num2 = rand();
printf("%d %d\n", num1, num2);
}
return 0;
}

summary
Level co., LTD. , Let's explain it in a more popular way .
And temporarily understood as :
1.
Provide to srand If the seed is fixed ,rand Never change .
2.
Using timestamp as seed , stay 1s Many times in Set by calling srand The starting point , actually srand every other 1s can By calling Change the starting point ,
Equivalent to the 1s The seed value is not changed in , Lead to 1s Set the starting point of the same seed multiple times , So the result is similar to 1 identical ,rand stay 1s Never change
3.
Using timestamp as seed , Set it up srand After the starting point , stay 1s Do not change the starting point or in 1s Then change the starting point ,rand The generated value is always a random value
4.
If not provided srand The starting point ,rand Although each generated value seems random , But in fact, when the program is run many times , It is found that after each run , The generated values correspond to each other and are equal .
5.
Using timestamp as seed ,srand Every time Recall settings The starting interval of is 1s! Attention is to recall ! But once the starting point is set , Each generation srand The starting interval is very small , I don't know the exact value, but it must be less than 1s.
Be careful : The most confusing point is !
By calling srand Function to set srand The starting point ,
Or through srand Set the starting point by changing the seed parameters of the function itself .
边栏推荐
- JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
- 中国能否在元宇宙的未来发展中取得突破,占领高地?
- leetcode day5 删除重复的电子邮箱
- Basic knowledge of communication network 01
- Function fitting based on MATLAB
- 并发程序设计,你真的懂吗?
- Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
- Labelme (I)
- Leetcode Day2 consecutive numbers
- Redis notes
猜你喜欢

河北:稳粮扩豆助力粮油生产提质增效

C+ + core programming

Advanced notes (Part 2)

How does app automated testing achieve H5 testing

editor.md中markdown编辑器的实现

Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022

NetCoreAPI操作Excel表格

Servlet learning notes

利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)

Cdga | how can the industrial Internet industry do a good job in data governance?
随机推荐
[网络]跨区域网络的通信学习IPv4地址的分类和计算
Advanced notes (Part 2)
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
Edge detection and connection of image segmentation realized by MATLAB
Leetcode day2 连续出现的数字
Leetcode Day5 delete duplicate email
Idea properties file display \u solution of not displaying Chinese
English translation Italian - batch English translation Italian tools free of charge
Intermediate soft test (system integration project management engineer) high frequency test site
Leetcode Day1 score ranking
一文读懂如何部署具有外部数据库的高可用 K3s
How does app automated testing achieve H5 testing
Getting started with enterprise distributed crawler framework
English Translation Spanish - batch English Translation Spanish tools free of charge
克服“看牙恐惧”,我们用技术改变行业
Leetcode Day2 consecutive numbers
C language function
Basic knowledge of C language
Source code analysis of scripy spider
时间转日期的sql语句应该怎么写?