生成随机数 - srand c++
我在使用 srand 时遇到问题。
我正在尝试生成一个 100 到 200 之间的随机数。
该数字将不断生成并放置在一个数组中。一旦再次调用该方法,就需要再次生成相同的随机数序列。
因此,我需要一颗种子,无论我如何尝试,我似乎都无法让它发挥作用。
我并不是在寻找任何人编写一些代码,而是只是向我展示生成此类数字的正确格式。
更新
我有一个火车对象,其中包含一个链接列表(内联列表中的每个位置都是一个车厢)。
每列火车的车厢数量需要在 100, 200 区间内随机。
每节车厢的煤炭量需要在 1000, 2000 区间内随机。
我正在尝试实现一个模拟器类,它将创建一列火车包含随机数据量的随机数量的车厢。
希望这更有意义。
正在为如何实施而苦恼。
I am having trouble using srand.
i am trying to generate a random number in the interval 100 to 200.
The number will keep being generated and placed in an array. Once the method is called again the same sequence of random numbers needs to be generated again.
Because of this I need a seed, no matter what I try I cannot seem to get it to work.
I am not looking for anyone to write some code rather just show me the correct formatting for generating such numbers.
UPDATE
I have a train object, which contains a linkedlist(each position in the linedlist is a carriage).
The number of carriages in each train needs to be random in the interval 100, 200.
The amount of coal in each carriage needs to be random in the interval 1000, 2000.
I am trying to implement a simulator class that will create a train with a random amount of carriages which contain a random amount of data.
Hope that makes a bit more sense.
Struggling on how to implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想重复任意序列,可以使用 srand() 为其提供相同的参数来设置种子。
例如:
If you just want to repeat an arbitrary sequence, you can set the seed with
srand()
by giving it the same argument.For example:
尝试一下
如果种子设置为 1,则生成器将重新初始化为其初始值,并生成与调用 rand 或 srand 之前相同的值。
try this
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
如前所述,您可以通过使用同一种子多次播种来进行粉碎。
rand()
的输出将从与之前相同的点重新开始。当您这样做时,我建议不要在 rand() 上使用模运算符,因为它不会导致值的均匀分布。相反,您可以使用以下帮助程序来获取整数范围内的随机值:
请参阅链接的有关 Eternally Confuzzled 的文章(作者:Julienne Walker)了解更多背景信息,以及播种。
C++ 选项
上面的缺点是生成完全是顺序的(不能同时拥有随机生成器的两个实例)。既然您使用 C++ 进行标记,我们为什么不使用它呢!
您可以使用 tr1 或 c++0x/c++111
uniform_int_distribution
:直接的好处是您可以让多个引擎同时生成相同的序列(请参阅示例)。
正如您所看到的,您可以像
engine(1234)
这样的生成器进行播种,就像使用srand
一样。查看实时示例:
带有 boost 的 C++03:https://ideone.com/FC4xm< /a>
<块引用>
C++0x http://ideone.com/467Aj
还演示了一些语法糖:
<块引用>
1 Boost.Random(如果使用 C++03)
As mentioned, you can srand by seeding it more than once with the same seed.
The output of
rand()
will be restarted from the same point as before.While you're at it, I recommend against using the modulo operator on rand(), because it will not result in a uniform distribution of value. Instead, you can use the following helper to get a random value in an integer range:
See the linked article on Eternally Confuzzled (by Julienne Walker) for more background, also on seeding.
C++ options
The above has the drawback that the generation is entirely sequential (you can not have two instances of the random generator at the same time). Since you are tagging with C++, why don't we use it!
You can use tr1 or c++0x/c++111
uniform_int_distribution
:An immediate advantage is that you can have multiple engines simultaneously generating the same sequence (see sample).
As you can also see, you can seed the generator like
engine(1234)
just like withsrand
.See the sample live on:
C++03 with boost: https://ideone.com/FC4xm
C++0x http://ideone.com/467Aj
Also demonstrating a little syntactic sugar:
1 Boost.Random if you use C++03