随机数种子面向.NET框架的相同版本 [英] Random number seed targeting the same version of the .NET framework

查看:125
本文介绍了随机数种子面向.NET框架的相同版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否每个CPU返回基于如果我的应用程序面向.NET Framework 3.5的相同的种子一样随机序列​​?如果你得到了相同的结果,我,我检查。我也希望大家谁分发我的应用程序将得到同样的结果。 !谢谢



 随机A =新的随机(44448); 
INT I1 = a.Next(65,90);
MessageBox.Show(i1.ToString());


解决方案

有关特定的框架版本的程序应该给予同样的每次运行它​​,因为固定的种子的时间造成的。



但它可以给不同版本的.NET框架的不同的结果。



例如,在.NET 4.0,我得到 77 。但是把代码放到 ideone (使用单声道)给出了 67



这样做的原因不同的是,因为随机使用的精确算法不是规范的一部分。 有大约算法此信息的文档:




当前执行Random类是基于唐纳德·E Knuth的消减随机数生成算法的修改版本。欲了解更多信息,请参见E. D.克努特。 计算机程序设计,第2卷的艺术:半数值算法。 Addison-Wesley出版社,阅读,MA,第二版,1981。



...



的实施在Random类随机数生成器的不能保证保持不变整个.NET Framework的主要版本。这样一来,你的应用程序代码不应该假设相同的种子将导致不同版本的.NET Framework的相同的伪随机序列。




重点煤矿。我们不能保证将来的实现将使用相同​​的算法。


Does every CPU return the same random sequence based on the same seed if my application targets .NET framework 3.5? I am checking if you get the same result as me. I am also hoping that everyone who I distribute my application to will get the same result. Thanks!

Random a = new Random(44448);
int i1 = a.Next(65, 90);
MessageBox.Show(i1.ToString());

解决方案

For a specific framework version your program should give the same result each time you run it, because of the fixed seed.

But it can give different results on different versions of the .NET framework.

For example, on .NET 4.0 I get 77. But putting the code into ideone (which uses Mono) gives 67.

The reason for this difference is because the precise algorithm used by Random is not part of the specification. The documentation has this information about the algorithm:

The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

...

The implementation of the random number generator in the Random class is not guaranteed to remain the same across major versions of the .NET Framework. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.

Emphasis mine. There are no guarantees that future implementations will use the same algorithm.

这篇关于随机数种子面向.NET框架的相同版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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