在 iOS 中播种 arc4random()
据我所知,arc4random()
生成的随机数比 rand()
好得多,但是我还没有找到一种方法来播种它,我想要就像使用srand()
一样。有办法吗?
From what I can gather arc4random()
generates much better random numbers than rand()
does, however I haven't seen a way to seed it, and I would like to just like using srand()
. Is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是 arc4random 的设计目的。正如文档所述:
由于无论如何它都是从熵源重新播种,因此手动播种不会获得任何好处,事实上,这样的方法不存在。
That's not what arc4random is designed to do. As the documentation states:
Since it is re-seeds itself from an entropy source anyway, you gain nothing by seeding it manually, and in fact, such a method does not exist.
实际上,您可以在 iOS 9 中执行此操作
。根据文档:
因此,只要您使用相同的种子数据(显然在生产代码中不使用
!
)和相同数量的丢弃值,您就会得到相同的结果。You can actually do this in iOS 9.
According to the docs:
So as long as you use the same seed data (obviously without using
!
in production code) and the same number of dropped values, you'll get the same results.在 Swift 3 中,当我需要种子值时,我使用
srand48()
和drand48()
。我制作的这个功能似乎足以满足我的需求:In Swift 3 I'm using
srand48()
anddrand48()
when I need a seeded value. I made this function that seems to work well enough for my needs:您可以使用以下命令将字节序列作为随机性添加到 arc4random:
arc4random_addrandom()
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/arc4random.3.html
You can add a byte sequence as randomness to arc4random by using:
arc4random_addrandom()
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/arc4random.3.html
您实际上不需要播种它......它会在第一次调用时自行播种。 来查看文档
通过调用shell 。描述下的相关行是:
You don't actually need to seed it... it seeds itself on the first call. Check out the documentation by calling
in your shell. The relevant line, under DESCRIPTION, is: