好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

实例讲解Java中random.nextInt()与Math.random()的基础用法

1、来源

random.nextint() 为 java.util.random类中的方法;

math.random() 为 java.lang.math 类中的静态方法。

2、用法

产生0-n的伪随机数(伪随机数参看最后注解):

?

1

2

3

// 两种生成对象方式:带种子和不带种子(两种方式的区别见注解)

random random = new random();

integer res = random.nextint(n);

?

1

integer res = ( int )(math.random() * n);

3、jdk源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

// random.nextint(n)

public int nextint( int n) {

   if (n <= 0 )

     throw new illegalargumentexception( "n must be positive" );

   if ((n & -n) == n) // i.e., n is a power of 2

     return ( int )((n * ( long )next( 31 )) >> 31 );

   int bits, val;

   do {

     bits = next( 31 );

     val = bits % n;

   } while (bits - val + (n- 1 ) < 0 );

   return val;

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// math.random()

public static double random() {

   random rnd = randomnumbergenerator;

   // 第一次调用,生成一个随机数生成器

   if (rnd == null ) rnd = initrng();

   return rnd.nextdouble();

}

// 生成的方法为同步的,线程安全

private static synchronized random initrng() {

  random rnd = randomnumbergenerator;

    return (rnd == null ) ? (randomnumbergenerator = new random()) : rnd;

  }

// 该方法为 random 类中的方法

public double nextdouble() {

   return ((( long )(next( 26 )) << 27 ) + next( 27 ))

     / ( double )(1l << 53 );

}

4、小结

math.random() 方法生成[0, 1)范围内的double类型随机数;random类中的nextxxxx系列方法生成0-n的随机数; math.random() 线程安全,多线程环境能被调用; 如无特殊需求,则使用(int)(math.random()*n)的方式生成随机数即可。

5、注:何谓伪随机数

伪随机既有规则的随机,random类中的随机算法就是伪随机。

具体表现为:相同种子数的random对象生成的随机数序列相同:

?

1

2

3

4

5

6

7

8

@test

public void createprojectno() {

    random r1 = new random( 100 );

    random r2 = new random( 100 );

    for ( int i = 0 ; i < 100 ; i ++) {

      system.out.println(r1.nextint( 10 )+ ", " +r2.nextint( 10 ));

    }

  }

结果为:

如不想生成相同的随机数序列,则应只使用一个random类。而math类中的随机数生成器 randomnumbergenerator 对象为静态的,可考虑使用。

6、注:random类的两种构造方法区别

1、源码

?

1

2

3

4

5

6

7

8

9

10

11

12

public random() {

  this (seeduniquifier() ^ system.nanotime());

}

public random( long seed) {

   if (getclass() == random. class )

     this .seed = new atomiclong(initialscramble(seed));

   else {

     // subclass might have overriden setseed

     this .seed = new atomiclong();

     setseed(seed);

   }

}

2、区别

从源码中可以看到,未定义种子的构造方法里,使用当前系统时间相关的一个数字作为种子数,该种子数只作为随机算法的起源数字,与生成的随机数区间无关系。

这篇文章是我对java中随机数的一些简单的理解,如有不对的地方或者其他的见解欢迎指导。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/u012099869/article/details/50394644

查看更多关于实例讲解Java中random.nextInt()与Math.random()的基础用法的详细内容...

  阅读:11次