Random r=new Random()
每次运行程序得到的随机数序列都是一样的。
例如第一次运行程序得到的随机数是 2, 4, 1, 5, 7
那么重启程序,再次得到的随机数还是2, 4, 1, 5, 7Random r=new Random(seedValue): 
每次运行程序时seedValue不一样,得到的随机数序列不一样,一般会这么用
Random r=new Random(System.nanoTime()): 

解决方案 »

  1.   


    2楼说反了吧。只有传入了固定的随机种子才能够得到固定的随机序列。Random产生的随机数实际上属于伪随机数,是按照一定算法计算出来的。构造方法如果没有传入随机种子的话,系统会默认采用系统时间作为随机种子。
    以下为Random类的源码
        /**
         * Creates a new random number generator. This constructor sets
         * the seed of the random number generator to a value very likely
         * to be distinct from any other invocation of this constructor.
         */
        public Random() { this(++seedUniquifier + System.nanoTime()); }
        private static volatile long seedUniquifier = 8682522807148012L;    /**
         * Creates a new random number generator using a single {@code long} seed.
         * The seed is the initial value of the internal state of the pseudorandom
         * number generator which is maintained by method {@link #next}.
         *
         * <p>The invocation {@code new Random(seed)} is equivalent to:
         *  <pre> {@code
         * Random rnd = new Random();
         * rnd.setSeed(seed);}</pre>
         *
         * @param seed the initial seed
         * @see   #setSeed(long)
         */
        public Random(long seed) {
            this.seed = new AtomicLong(0L);
            setSeed(seed);
        }
      

  2.   

    2楼说反了吧。只有传入了固定的随机种子才能够得到固定的随机序列。Random产生的随机数实际上属于伪随机数,是按照一定算法计算出来的。构造方法如果没有传入随机种子的话,系统会默认采用系统时间作为随机种子。
    以下为Random类的源码
        /**
         * Creates a new random number generator. This constructor sets
         * the seed of the random number generator to a value very likely
         * to be distinct from any other invocation of this constructor.
         */
        public Random() { this(++seedUniquifier + System.nanoTime()); }
        private static volatile long seedUniquifier = 8682522807148012L;    /**
         * Creates a new random number generator using a single {@code long} seed.
         * The seed is the initial value of the internal state of the pseudorandom
         * number generator which is maintained by method {@link #next}.
         *
         * <p>The invocation {@code new Random(seed)} is equivalent to:
         *  <pre> {@code
         * Random rnd = new Random();
         * rnd.setSeed(seed);}</pre>
         *
         * @param seed the initial seed
         * @see   #setSeed(long)
         */
        public Random(long seed) {
            this.seed = new AtomicLong(0L);
            setSeed(seed);
        }

    2楼说反了吧。只有传入了固定的随机种子才能够得到固定的随机序列。Random产生的随机数实际上属于伪随机数,是按照一定算法计算出来的。构造方法如果没有传入随机种子的话,系统会默认采用系统时间作为随机种子。
    以下为Random类的源码
        /**
         * Creates a new random number generator. This constructor sets
         * the seed of the random number generator to a value very likely
         * to be distinct from any other invocation of this constructor.
         */
        public Random() { this(++seedUniquifier + System.nanoTime()); }
        private static volatile long seedUniquifier = 8682522807148012L;    /**
         * Creates a new random number generator using a single {@code long} seed.
         * The seed is the initial value of the internal state of the pseudorandom
         * number generator which is maintained by method {@link #next}.
         *
         * <p>The invocation {@code new Random(seed)} is equivalent to:
         *  <pre> {@code
         * Random rnd = new Random();
         * rnd.setSeed(seed);}</pre>
         *
         * @param seed the initial seed
         * @see   #setSeed(long)
         */
        public Random(long seed) {
            this.seed = new AtomicLong(0L);
            setSeed(seed);
        }
    还是不知道有什么用