rand()可以用来生成可预测的数据吗? [英] can rand() be used to generate predictable data?

查看:133
本文介绍了rand()可以用来生成可预测的数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是生成2D或3D几何图形而不必将其存储在磁盘上,因此我的目标是具有某种功能,而不是根据一粒小种子生成相同的值.我不是要寻找随机值,但是如果给定相同的种子时返回相同的随机"垃圾数据,那就是我要寻找的东西.

My goal is generate 2D or 3D geometry without having to store it on disk, so my goal is to have any sort of function than generate the same values according to a small seed. I don't mean to seek random values, but if the same "random" garbage data is returned when given the same seed, that's something I'm looking for.

如果我给srand()相同的整数,我将从rand()中得到相同的序列.那是预期的功能吗?如果不是,是否有已知的标准功能旨在完成相同的工作?

If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature ? If not, are there known standard functions designed to do the same thing ?

尽管我在ideone和计算机上进行了尝试,但得到了不同的结果,但我可以理解,这些功能的实现未进行描述,因此可以对其进行解释.

Although I tried this on ideone and on my computer and I get different results, I can understand that those function's implementations are not described, so that explains it.

推荐答案

如果我给srand()相同的整数,我将从rand()中得到相同的序列.这是预期的功能吗?

If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature?

是,请参阅7.20.2.2:

Yes, see 7.20.2.2:

7.20.2.2 srand函数

[...]说明

srand函数使用该参数作为新伪随机序列的种子 后续调用rand返回的数字. 如果随后使用srand调用 相同的种子值,应重复伪随机数的序列.

7.20.2.2 The srand function

[...] Description

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated.

但是,这仅适用于srand/rand的相同实现.另一个实现可能不会使用相同的算法,因此不会产生相同的序列.

However, that's only true for the same implementation of srand/rand. Another implementation might not use the same algorithm, and therefor won't produce the same sequence.

如果没有,是否有已知的标准功能旨在完成相同的工作?

If not, are there known standard functions designed to do the same thing ?

好,这些函数是标准的,但仅在其行为上,而不是实际值(请参见上面的实现说明).您最好使用 C ++ 11预定义随机数生成器,因为它们是标准化的.

Well, the functions are standard, but only in their behaviors, not the actual values (see implementation remark above). You're better off by using a specific generator from the C++11 predefined random number generators, since they're standardized.

这篇关于rand()可以用来生成可预测的数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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