简体   繁体   English

如何不使用随机库等构建随机数?

[英]How ı build random number not using random library or etc.?

I have to generate random numbers in two different places in the project and I do it using the system clock, but I have to get different numbers in both.我必须在项目的两个不同位置生成随机数,并且使用系统时钟来生成随机数,但我必须在两者中获得不同的数字。 How can I do that?我怎样才能做到这一点?

You could use a loop surrounded by calls to System.nanoTime() , then use the least significant bits of their difference as random seed.您可以使用由调用System.nanoTime()包围的循环,然后使用它们差异的最低有效位作为随机种子。 Once you have, say, 128 bits you can use it as seed for a pseudo random number generator.例如,一旦拥有 128 位,您就可以将其用作伪随机数生成器的种子。

There is an article posted in the wiki at java-gaming.org about generating pseudo-random numbers that might be worth perusing.在 java-gaming.org 的 wiki 上发布了一篇关于生成可能值得细读的伪随机数的文章。 Pseudo-random number generators He shows what he calls an "Old School" method of creating them using a combination of polynomials and modular division. 伪随机数生成器他展示了一种他称之为“老派”的方法,该方法使用多项式和模除法的组合来创建它们。 Plus links to other types of PRNGs.加上其他类型的 PRNG 的链接。

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

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