为什么这段代码会给出奇怪的结果?好随便喔? [英] Why this code is giving strange result? So Random?

查看:91
本文介绍了为什么这段代码会给出奇怪的结果?好随便喔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以生成一些随机数并在控制台上打印出来.但是,我对它所打印的图案感到好奇,例如

I have piece of code which generates some Random number and prints out on console. However I am curious about the pattern which it prints, Such as,

import java.util.*;
public class Test
{
    public static void main(String[] args)
    {
        Random random = new Random(-6732303926L);
            for(int i=0;i<10;i++)
                System.out.println(random.nextInt(10)+" ");    
    }
}

结果:0 1 2 3 4 5 6 7 8 9 -换行中的每个数字.

Result : 0 1 2 3 4 5 6 7 8 9 - Every number in new line.

如果您稍稍更改此代码,就可以了!

And if you change this code a bit! like,

import java.util.*;
public class Test
{
    public static void main(String[] args)
    {
        Random random = new Random(-6732303926L);
            for(int i=0;i<10;i++)
                System.out.println(random.nextInt(11)+" ");    
    }
}

结果:8 9 2 2 10 3 8 7 0 10 -新行中的每个数字.

Result : 8 9 2 2 10 3 8 7 0 10 - Every number in new line.

0123456789根本不是随机的原因是什么??

推荐答案

序列的原因是,人们可以使用其下一种方法,使Random可预测具有可预测和可重复的序列,从而对软件进行测试.每当特定的长种子参数是Random构造函数的参数时,实例化的Random对象都应该通过其next方法返回相同的值序列.这是java.util.Random的故意功能.

The reason for the sequence is so that one can test software by making Random predicable with predictable and repeatable sequences using its next methods. Whenever a particular long seed parameter is a parameter to the Random constructor, the instanced Random object is supposed to return the same sequences of values through its next methods. This is a deliberate feature of java.util.Random.

java.util.Random有两个构造函数:

java.util.Random has two constructors:

Random()

Random(long seed)

没有长整数种子的构造函数使用系统时间为伪随机数生成器创建种子值.没有两个Random实例化将使用相同的种子,并且您应该获得一个非常好的伪随机序列.使用没有种子的构造函数进行的随机实例化将创建一个实例,该实例具有不可预测的值序列,这些值将是伪随机的.

The constructor without a long integer seed uses the system time for creating a seed value for the pseudo random number generator. No two instantiations of Random will use the same seed and you should get a very good pseudo-random sequence. A Random instantiation using the constructor without a seed creates an instance with unpredictable sequences of values that will be pseudo-random.

具有种子值的构造函数仅用于使用其下一种方法对具有可预测序列的Random确定性.种子的典型用途是用于软件测试,其中结果必须是可预测的和可重复的.每个使用相同的长种子整数的Random实例每次都会创建相同的结果序列.当使用nextInt(10)方法获取10个整数值之一时,所使用的特定长度会导致该序列一次又一次地变为0 1 2 3 4 5 6 7 8 9.每次执行时可重复的此可预测序列和其他可预测序列对于测试软件非常有用,而不是用于创建不可预测的伪随机序列.

The constructor with a seed value is intended only for making Random deterministic with predictable sequences using its next methods. The typical use of a seed is for software test purposes where results must be predicable and repeatable. Every instance of Random that uses the same long seed integer will create the same sequence of results every time. The particular long you used causes the sequence to be 0 1 2 3 4 5 6 7 8 9 over and over again when getting one of 10 integer values using nextInt(10) method. This and other predictable sequences that are repeatable every time software executes are very useful for testing software and are not meant for creating unpredictable pseudo-random sequences.

这篇关于为什么这段代码会给出奇怪的结果?好随便喔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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