种子在随机数生成中的作用

编程入门 行业动态 更新时间:2024-10-27 17:21:52
本文介绍了种子在随机数生成中的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在脑海中有一个大问题:

I have a big question in my mind:

我可以使用种子数生成随机数:

I can use a seed number to generate random numbers:

Random rand = new Random(34666666);

但是我无法理解的是种子的作用.例如

But the thing that I cannot understand is the role of that seed. For example what is the difference of

该代码包含以下内容:

Random rand = new Random();

推荐答案

当您提供特定的,硬编码的种子时,请提供给one-arg Random构造函数,每次运行程序时,将生成的随机数始终相同.当您需要可预测的随机数源时就需要这样做.

When you supply a specific, hard-coded seed, to the one-arg Random constructor, the random numbers that will be generated will always be the same, every time you run the program. That is needed when you need a predictable source of random numbers.

但是,当您不提供种子时,请 Random构造函数将根据System.nanoTime为您选择一个种子.每次运行程序时,随机数都会不同,因为种子每次都会不同.

However, when you don't supply a seed, then the Random constructor will choose a seed for you, based on System.nanoTime. The random numbers will be different every time you run the program, because the seed will be different each time.

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

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.

这很重要,因为Java的随机数生成器是伪随机的.每个新的伪随机数都会影响用于生成下一个伪随机数的种子.

This is important because Java's random number generator is pseudo-random; each new pseudo-random number affects the seed that is used for the next pseudo-random number that gets generated.

更多推荐

种子在随机数生成中的作用

本文发布于:2023-11-08 08:09:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   种子   作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!