为什么 random 在这里表现不同? [英] Why random is behaving differently here?

查看:50
本文介绍了为什么 random 在这里表现不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释为什么下面的代码给出输出0 1 2 3 4 5 6 7 8 9.

Can anybody please explain why the below code gives output 0 1 2 3 4 5 6 7 8 9.

 Random random = new Random(-6732303926L);
    for(int i=0;i<10;i++)
        System.out.println(random.nextInt(10)+" ");

从 Java API Random 我可以请参阅构造函数 Random(long seed) 使用单个 long seed 创建一个新的随机数生成器.那么,这又会是随机的,为什么每次都给出一个固定的输出?

From Java API Random I can see constructor Random(long seed) creates a new random number generator using a single long seed. So, this will be random again, why this gves a fixed output every time?

推荐答案

从 Java API Random 我可以看到构造函数 Random(long seed) 使用单个长种子创建了一个新的随机数生成器.那么,这又会是随机的,为什么每次都给出一个固定的输出?

From Java API Random I can see constructor Random(long seed) creates a new random number generator using a single long seed. So, this will be random again, why this gves a fixed output every time?

seed 用于在不同的运行中生成相同的输出(来自随机操作)该程序.因此,每次使用相同的种子创建 Random 对象时,都会输出相同的结果.

A seed is used to generate the same output (from random operations) in different runs of the program. So, each time you create the Random object with the same seed, it will output the same result.

注意:您选择的种子有一个特定的输出,即 0 1 2 3 4 5 6 7 8 9,但这并不意味着什么.还会有其他一些具有相同输出的种子.

Note: The seed you chose has a particular output, that is 0 1 2 3 4 5 6 7 8 9, but it doesn't mean anything. There will also be some other seeds with that same output.

这篇关于为什么 random 在这里表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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