問題描述
就生成隨機數而言,種子是什么?
What is a seed in terms of generating a random number?
我需要生成數百到數千個隨機數,我已經閱讀了很多關于使用種子"的信息.什么是種子?是隨機數從哪里開始的種子嗎?例如,如果我將種子設置為 5,它會生成從 5 到我的限制的數字嗎?例如,它永遠不會給我 3.
I need to generate hundreds to thousands of random numbers, I have read a lot about using a "seed". What is a seed? Is a seed where the random numbers start from? For example if I set my seed to be 5 will it generate numbers from 5 to whatever my limit is? So it will never give me 3 for example.
我使用的是 C++,所以如果你提供任何示例,如果它是在 C++ 中就好了.
I am using C++, so if you provide any examples it'd be nice if it was in C++.
謝謝!
推薦答案
現實中通常所說的隨機數序列是偽隨機"數序列,因為這些值是使用確定性算法計算的,而概率沒有實際作用角色.
What is normally called a random number sequence in reality is a "pseudo-random" number sequence because the values are computed using a deterministic algorithm and probability plays no real role.
種子"是序列的起點,保證如果您從同一個種子開始,您將獲得相同的數字序列.例如,這對于調試非常有用(當您在程序中查找錯誤時,您需要能夠重現問題并研究它,非確定性程序將更難調試,因為每次運行都會不同).
The "seed" is a starting point for the sequence and the guarantee is that if you start from the same seed you will get the same sequence of numbers. This is very useful for example for debugging (when you are looking for an error in a program you need to be able to reproduce the problem and study it, a non-deterministic program would be much harder to debug because every run would be different).
如果您只需要一個隨機的數字序列并且不需要重現它,那么只需使用當前時間作為種子...例如:
If you need just a random sequence of numbers and don't need to reproduce it then simply use current time as seed... for example with:
srand(time(NULL));
這篇關于就生成隨機數而言,種子是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!