可以在Objective-C中保存种子PRNG吗?

时间:2023-01-15 14:11:43

I'm seeding a PRNG in objective-c by writing

我正在通过写作为目标-c播种PRNG

srandom(seed);

I'n C# I can save that PRNG away in a variable as:

我是C#我可以将PRNG保存在变量中:

prng = new Random(seed);

Is this possible in Objective-C as well?

这在Objective-C中是否可行?

The reason I want to save the PRNG in a variable is that a want a deterministic outcome on numbers generated immediately and later. I don't want to have the risk that another function reseeds the PRNG.

我想将PRNG保存在变量中的原因是想要立即和稍后生成的数字的确定性结果。我不希望有另一个功能重新组合PRNG的风险。

I have tried to do some research, for example reading this article that lists different libraries: http://nshipster.com/random/, but I haven't found anything.

我曾尝试做一些研究,例如阅读这篇列出不同库的文章:http://nshipster.com/random/,但我没有找到任何东西。

3 个解决方案

#1


1  

In C, srand() and rand() are not related to objects, like in C#. srand() simply sets a global variable, and rand() reads from it. As far as I remember, Objective-C uses the original, C implementation for this library. Since there's no objects involved, every call to the function rand() will always access the same PRNG seed set by srand().

在C中,srand()和rand()与对象无关,就像在C#中一样。 srand()只是设置一个全局变量,rand()从中读取。据我所知,Objective-C使用了这个库的原始C实现。由于没有涉及对象,因此对函数rand()的每次调用将始终通过srand()访问相同的PRNG种子集。

This doesn't mean there aren't any solutions though. First (maybe not the best solution) is writing your own PRNG as an object. Another option would be to find an object-orientated, PRNG library (I normally don't code in Objective-C so I don't have any recommendations). Finally, you could follow the standard generally used with rand(), and only call srand() once, at the beginning of your program, if that is appropriate for your application.

这并不意味着没有任何解决方案。首先(可能不是最佳解决方案)是将您自己的PRNG编写为对象。另一种选择是找到一个面向对象的PRNG库(我通常不在Objective-C中编码,因此我没有任何建议)。最后,您可以遵循通常与rand()一起使用的标准,并且只在程序开始时调用srand()一次,如果它适合您的应用程序。

#2


1  

The GameplayKit Foundation library provides class GKMersenneTwisterRandomSource. You can create independently seeded multiple instances using initWithSeed:, and pass them around to your heart's content.

GameplayKit Foundation库提供了GKMersenneTwisterRandomSource类。您可以使用initWithSeed:创建独立种子的多个实例,并将它们传递给您的心脏内容。

#3


0  

Use Below code It may help you to save random number :

使用下面的代码它可以帮助您保存随机数:

    unsigned int seed = 13;  /* Choose value for the seed */
    int random;

    srand(seed);             /* Set the seed                           */
    random = rand();         /* Generate a random number               */

Use srand to set the seed, and then use rand()

使用srand设置种子,然后使用rand()

Hopw it will help you. Do let me know if your problem is solved or not.

它会帮助你。如果您的问题得到解决,请告诉我。

#1


1  

In C, srand() and rand() are not related to objects, like in C#. srand() simply sets a global variable, and rand() reads from it. As far as I remember, Objective-C uses the original, C implementation for this library. Since there's no objects involved, every call to the function rand() will always access the same PRNG seed set by srand().

在C中,srand()和rand()与对象无关,就像在C#中一样。 srand()只是设置一个全局变量,rand()从中读取。据我所知,Objective-C使用了这个库的原始C实现。由于没有涉及对象,因此对函数rand()的每次调用将始终通过srand()访问相同的PRNG种子集。

This doesn't mean there aren't any solutions though. First (maybe not the best solution) is writing your own PRNG as an object. Another option would be to find an object-orientated, PRNG library (I normally don't code in Objective-C so I don't have any recommendations). Finally, you could follow the standard generally used with rand(), and only call srand() once, at the beginning of your program, if that is appropriate for your application.

这并不意味着没有任何解决方案。首先(可能不是最佳解决方案)是将您自己的PRNG编写为对象。另一种选择是找到一个面向对象的PRNG库(我通常不在Objective-C中编码,因此我没有任何建议)。最后,您可以遵循通常与rand()一起使用的标准,并且只在程序开始时调用srand()一次,如果它适合您的应用程序。

#2


1  

The GameplayKit Foundation library provides class GKMersenneTwisterRandomSource. You can create independently seeded multiple instances using initWithSeed:, and pass them around to your heart's content.

GameplayKit Foundation库提供了GKMersenneTwisterRandomSource类。您可以使用initWithSeed:创建独立种子的多个实例,并将它们传递给您的心脏内容。

#3


0  

Use Below code It may help you to save random number :

使用下面的代码它可以帮助您保存随机数:

    unsigned int seed = 13;  /* Choose value for the seed */
    int random;

    srand(seed);             /* Set the seed                           */
    random = rand();         /* Generate a random number               */

Use srand to set the seed, and then use rand()

使用srand设置种子,然后使用rand()

Hopw it will help you. Do let me know if your problem is solved or not.

它会帮助你。如果您的问题得到解决,请告诉我。