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

2023-06-05C/C++开发问题
20

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

问题描述

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

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

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6