带种子的随机数

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

参考:链接文字

我无法理解以下内容,有人能为我提供以下声明的一些例子吗?

i cannot understand the following line , can anybody provide me some example for the below statement?

如果有两个Random的实例是使用相同的种子创建的,并且每个方法调用相同的序列,它们将生成并返回相同的数字序列

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers

推荐答案

因为你问了一个例子:

import java.util.Random; public class RandomTest { public static void main(String[] s) { Random rnd1 = new Random(42); Random rnd2 = new Random(42); System.out.println(rnd1.nextInt(100)+" - "+rnd2.nextInt(100)); System.out.println(rnd1.nextInt()+" - "+rnd2.nextInt()); System.out.println(rnd1.nextDouble()+" - "+rnd2.nextDouble()); System.out.println(rnd1.nextLong()+" - "+rnd2.nextLong()); } }

两者随机实例将始终具有相同的输出,无论您运行它的频率如何,无论您使用什么平台或Java版本:

Both Random instances will always have the same output, no matter how often you run it, no matter what platform or what Java version you use:

30 - 30 234785527 - 234785527 0.6832234717598454 - 0.6832234717598454 5694868678511409995 - 5694868678511409995

更多推荐

带种子的随机数

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

发布评论

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

>www.elefans.com

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