Microsoft是否更改了随机默认种子? [英] Did Microsoft change Random default seed?

查看:109
本文介绍了Microsoft是否更改了随机默认种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我在.NET Core中进行了一些测试,并且遇到了一些有趣的事情.

Today, I was doing some tests in .NET Core, and I have come across some interesting thing.

在(.NET Framework 4)之前,Random使用Environment.TickCount,但是现在我相信情况已经改变.

Before (~ .NET Framework 4), Random used Environment.TickCount, but now I believe this has changed.

考虑以下代码:

while (true)
{
    Random random = new Random();
    Console.WriteLine(random.Next(0, 10000));
}

在较早的.NET Framework版本中,new Random()空构造函数将使用Environment.TickCount,这将导致伪随机值的重复.

In older .NET Framework versions, the new Random() empty constructor would use Environment.TickCount, which would lead to repetition of pseudo-random values.

所以您可以期望得到如下结果:

So you could expect results like:

542
4211
5244
5244
5244
9501
9501

以此类推.

在使用最新编译器的最新.NET Core版本上,我收到以下结果:

On the latest .NET Core version using the latest compiler, I have received the following result:

5332
220
3928
524
2973
2840
4965
5667
657
6434
3170
3046
7044

绝对可以改善的地方.

其他在旧版本中表现出此行为的S.O问题:

Other S.O questions demonstrating this behaviour in older versions:

如何生成随机的整数?

生成在C#中没有重复的随机数

非重复随机数

C#随机数生成器线程安全吗?

我的设置:.NET Core 2.2/最新的C#编译器.

My setup: .NET Core 2.2 / latest C# compiler.

实际问题

所以我的问题是,PRNG确实得到了改善,还是他们只是将构造函数更改为使用其他默认种子,如果是,则它们用作种子是什么?密码术现在是否更安全(如果他们实际上改变了实现方式)?

So my question is, has the PRNG really improved or they just changed constructor to use another default seeds, and, if so, what they're using as a seed? Is it safer now for cryptography (if they actually changed the implementation)?

推荐答案

在最新版本的dotnet核心中,Random默认构造函数从Random的隐藏私有实例分配其种子.私有实例使用Interop.GetRandomBytes作为其种子.新实例将专用实例的Next()结果用作其种子.

In the latest version of dotnet core, the Random default constructor assigns its seed from a hidden private instance of Random. The private instance uses Interop.GetRandomBytes for its seed. New instances use the private instances's Next() result as its seed.

从本质上讲,这使在循环中创建多个随机实例变得安全".

This basically makes it 'safe' to create several random instances in a loop.

有关corefx GitHub的更多信息:

Read more on the corefx GitHub:

相关代码文件:私有随机实例私有随机实例-生成种子.

种子更改请求无参数构造函数播种改进#1919

这篇关于Microsoft是否更改了随机默认种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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