Java中的默认种子PRNG [英] Default seed PRNG in Java

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

问题描述

我想知道Java中的 Math.random()后面的PRNG *的默认种子是什么。根据我的理解,C中的那个基于系统时钟。它在Java中是否相似?此外,是否每次更改种子 Math.random()

I was wondering what the default seed for the PRNG* behind Math.random() in Java is. From what I understand the one in C is based upon the system clock. So is it similar in Java? Also, is the seed changed everytime Math.random() is called?

* PRNG =伪随机数生成器

*PRNG = Pseudo Random Number Generator

推荐答案

如果你阅读精细手册它告诉你


首次调用此方法时,它会创建一个新的
伪随机数生成器,就像表达式

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random()

此新的伪随机数生成器用于此后对此方法的所有调用
并且在其他任何地方都没有使用。

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

跟进 java.util.Random(),文档说


public Random()

创建一个新的随机数生成器。此构造函数将随机数生成器的种子
设置为与此构造函数的任何其他调用非常可能不同的值

Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

当前的实现似乎基于 System.nanoTime(),但可能会更改并仍然符合文档的合同。

The current implementation appears to be based on System.nanoTime() but could change and still be compliant with the documentation's contract.

至于每次通话都要改变种子,那不是种子的运作方式。 PRNG被播种一次,然后产生一系列从初始状态演变而来的值。你不应该,Java不会,继续重播。

As for changing the seed with every call, that's not how seeds work. PRNGs are seeded once, and then produce a sequence of values that evolve from that initial state. You shouldn't, and Java doesn't, keep re-seeding.

这篇关于Java中的默认种子PRNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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