带种子的随机数 [英] random number with seed

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

问题描述

参考:链接文字

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

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

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

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