Why this code is giving strange result? So Random?

2019-03-06 06:09发布

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)+" ");    
    }
}

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)+" ");    
    }
}

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

What is the reason of 0123456789 which is not random at all!?

标签: java random
6条回答
狗以群分
2楼-- · 2019-03-06 06:23

Creating random with a seed ensures a certain behavior. Especially, creating two instances of Random with the same seed, will always behave identically. Someone found out (via brute force, I guess) that using this particular seed together with the first 10 nextInt(10), creates such a seemingly ordered sequence. This sequence is pseudo-random updon first creation, but can be reproduced. Changing anything in the slightest gives a different result.

查看更多
叼着烟拽天下
3楼-- · 2019-03-06 06:25

The Random class is a pseudo-random number generator. Thus it is not truely random, but instead relies on mathematical operations performed on an initial seed value. Thus, certain seeds will produce certain (potentially interesting/fun) sequences

查看更多
爷、活的狠高调
4楼-- · 2019-03-06 06:37

I think it is random... you are using a specific seed for the random function. You just found the seed that will give you the numbers 0 - 9, in order.

EDIT: Apparently, this is the algorithm:

The java.util.Random class implements what is generally called a linear congruential generator (LCG). An LCG is essentially a formula of the following form: numberi+1 = (a * numberi + c) mod m

Source: here

查看更多
在下西门庆
5楼-- · 2019-03-06 06:45

Random is based on the seed you give to it, if you want to get true random numbers, use time functions as seeds, and you'll get a real random number series.

查看更多
ら.Afraid
6楼-- · 2019-03-06 06:47

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 has two constructors:

Random()

and

Random(long seed)

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.

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.

查看更多
Lonely孤独者°
7楼-- · 2019-03-06 06:47

0123456789 is random too, in this case - it's about as likely to come up as 14235682907, which would no doubt not have given you any cause for concern.

You spotted a fluke, basically. If you print the next 10 numbers in the first case, they're not preserving any obvious order.

It's like flipping a coin - the pattern HHHHHHHH is just as likely to come up as the exact pattern HHTHTTHH; there's a 1 in 28 chance of each coming up, as at any of the 8 steps there's a 50% chance of it going wrong. But the first pattern looks like it's broken, whereas the second doesn't.

查看更多
登录 后发表回答