在 iOS 中播种 arc4random()

发布于 2024-10-04 02:22:34 字数 139 浏览 3 评论 0原文

据我所知,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦行七里 2024-10-11 02:22:34

这不是 arc4random 的设计目的。正如文档所述:

arc4random() 函数提供高质量的 32 位伪随机数
数量很快。 arc4random() 定期从
random(4) 中描述的内核强随机数子系统。

由于无论如何它都是从熵源重新播种,因此手动播种不会获得任何好处,事实上,这样的方法不存在。

That's not what arc4random is designed to do. As the documentation states:

The arc4random() function provides a high quality 32-bit pseudo-random
number very quickly. arc4random() seeds itself on a regular basis from
the kernel strong random number subsystem described in random(4).

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.

尹雨沫 2024-10-11 02:22:34

实际上,您可以在 iOS 9 中执行此操作

import GameKit

let source = GKARC4RandomSource(seed: "hello world".data(using: .utf8)!)
source.dropValues(1024)
source.nextInt() // <-- your number

。根据文档:

基于 Arc4 的随机源具有可重复的初始序列。如果用于混淆,您应该从一开始就删除 N 个值,其中 N 应该是大于 768 的任何数字,以确保刷新初始序列。

因此,只要您使用相同的种子数据(显然在生产代码中不使用 !)和相同数量的丢弃值,您就会得到相同的结果。

You can actually do this in iOS 9.

import GameKit

let source = GKARC4RandomSource(seed: "hello world".data(using: .utf8)!)
source.dropValues(1024)
source.nextInt() // <-- your number

According to the docs:

Arc4 based random sources have repeatable initial sequences. If used for obfuscation you should drop N values from the start, where N should be any number larger than 768 to ensure the initial sequence is flushed.

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.

魄砕の薆 2024-10-11 02:22:34

在 Swift 3 中,当我需要种子值时,我使用 srand48()drand48() 。我制作的这个功能似乎足以满足我的需求:

func seeded_rand(seed:Int, min:Double, max:Double) -> Int
{
    srand48(seed)
    return Int(round(drand48() * (max-min)) + min)
}

In Swift 3 I'm using srand48() and drand48() when I need a seeded value. I made this function that seems to work well enough for my needs:

func seeded_rand(seed:Int, min:Double, max:Double) -> Int
{
    srand48(seed)
    return Int(round(drand48() * (max-min)) + min)
}
节枝 2024-10-11 02:22:34

您可以使用以下命令将字节序列作为随机性添加到 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

北城孤痞 2024-10-11 02:22:34

您实际上不需要播种它......它会在第一次调用时自行播种。 来查看文档

man arc4random

通过调用shell 。描述下的相关行是:

There is no need to call arc4random_stir() before using arc4random(),
since arc4random() automatically initializes itself.

You don't actually need to seed it... it seeds itself on the first call. Check out the documentation by calling

man arc4random

in your shell. The relevant line, under DESCRIPTION, is:

There is no need to call arc4random_stir() before using arc4random(),
since arc4random() automatically initializes itself.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文