就生成随机数而言,种子是什么? [英] What is a seed in terms of generating a random number?

查看:27
本文介绍了就生成随机数而言,种子是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就生成随机数而言,种子是什么?

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));

这篇关于就生成随机数而言,种子是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆