我如何从已经创建的随机数中获取种子 [英] How can I get the seed from a Random already created

查看:37
本文介绍了我如何从已经创建的随机数中获取种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须能够重复使用我的代码进行的实验,它会产生一些随机数,并且我需要获取新的random()语句的初始化值.我有这句话可以使我使用的随机对象

I have to be able to repeat an experiment made with my code, it makes a few random numbers and I need to get the initialization value of my new random() sentence. I have this sentence to make the random object I use

Dim r As Random = New Random()

如我所读,它从系统日期时间获取初始化值.如果实验成功,我需要能够重复一次.我该如何获取初始化编号:

As I have read it gets the initialization value from system datetime. If the experiment were successfull I'd needed to be able to repeat it. How could I get the initialization number to be able to do:

 Dim r As Random = New Random(OldInitializationValue)

我认为每次执行我的应用程序时都必须自己设定一个初始化值,但是如果有一种保存方法,我将很高兴知道它.

I think I'll have to make a Initialization value myself each time I execute my app but If there were a method to save it I'd be glad to know it.

预先感谢您的回答.

推荐答案

Random 不公开初始种子,甚至不存储初始种子,因此无法检索它.因此,您必须使用构造器,该种子会存储它自己.

Random does not expose the initial seed, it doesn't even store it so there is no way to retrieve it. So you have to use the constructor that takes the seed and store it yourself.

Dim mySeed As Int32 = Environment.TickCount ' store it somewhere '

Dim myRandom = New Random(mySeed)
For i As Int32 = 1 To 10
    Console.WriteLine(myRandom.Next(1, 100))
Next

' somehwere else
myRandom = New Random(mySeed)
For i As Int32 = 1 To 10
    Console.WriteLine(myRandom.Next(1, 100))
Next

现在,您始终可以使用相同的种子创建一个随机实例,以获取相同的伪随机值序列.

Now you can always create a random instance with the same seed to get the same sequence of pseudo random values.

这篇关于我如何从已经创建的随机数中获取种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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