简体   繁体   English

根据种子值生成随机数

[英]Generate random numbers based off of a seed value

Someone brought up the idea of generating random numbers in the exact same order based off of a seed value, and I started thinking extremely hard on how to do this. 有人提出了基于种子值以完全相同的顺序生成随机数的想法,而我开始非常努力地思考如何做到这一点。 We challenged eachother to create an application that does the following: 我们互相挑战,以创建一个执行以下操作的应用程序:

Generate a set of five random numbers ranging from 1 to 100 every ten seconds based on a seed value. 基于种子值,每十秒生成一组五个随机数,范围从1到100。 The numbers generated (while using the same seed value) should be exactly the same and generated in exactly the same order, therefor if the application is ran for 20 seconds and the numbers [1, 17, 2, 58, 27, 83, 32, 56, 27, 4] are generated, if the application is restarted these exact same numbers should be generated after 20 seconds, if the same seed was provided. 生成的数字(使用相同的种子值时)应完全相同,并以完全相同的顺序生成,因此,如果应用程序运行了20秒,并且数字[1、17、2、58、27、83、32 ,56、27、4]生成,如果重新启动应用程序,则如果提供了相同的种子,则应该在20秒后生成这些完全相同的数字。

This will allow for multiple clients to generate the same exact information based off of a single numeric seed value. 这将允许多个客户端根据单个数字种子值生成相同的确切信息。

Unfortunately after a few days we've both ended up falling short and we're completely clueless as to how to do this. 不幸的是,几天之后,我们俩都落空了,我们对如何做到这一点一无所知。 We don't even really know what the proper term for this type of behavior is, however I've called it "Deterministic number generation" 我们甚至不知道这种行为的正确用语是什么,但是我称其为“确定性数生成”

I've tagged both languages that we tried using for this expirement, hoping someone can help us out. 我已经标记了我们尝试使用的两种语言,希望有人可以帮助我们。 This would be a very interesting block of code to experiment with, and we've even thought of ways to improve some of our mobile games by using this strategy, if we can find a way to do it. 这将是一个非常有趣的代码块,可以尝试,而且,即使我们能够找到一种方法,我们甚至已经考虑过使用此策略来改进某些手机游戏的方法。

Any help would be graciously appreciated. 任何帮助将不胜感激。

At risk of stating the obvious, but in C# this would be - 冒着陈述显而易见的风险,但是在C#中,这是-

int seed = 12;
Random r = new Random(seed);

for (int n = 0; n < 20; n++)
{
    Console.WriteLine("{0}", r.Next(1, 100));
}

Where as long as you both use the same seed number you should get the same list ... 只要你们俩使用相同的种子编号,您就应该获得相同的列表...

Unless I a missing something in your question here ... 除非我在这里在您的问题中遗漏了一些东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM