Java生成随机数

Posted 高数考了59

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java生成随机数相关的知识,希望对你有一定的参考价值。

Math.random() 会得到一个0-1之间的随机浮点数

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] a = new int[5];
 4         a[0] = (int) (Math.random() * 100);
 5         a[1] = (int) (Math.random() * 100);
 6         a[2] = (int) (Math.random() * 100);
 7         a[3] = (int) (Math.random() * 100);
 8         a[4] = (int) (Math.random() * 100);
 9          
10         System.out.println("数组中的各个随机数是:");
11         for (int i = 0; i < a.length; i++)
12             System.out.println(a[i]);

以上是关于Java生成随机数的主要内容,如果未能解决你的问题,请参考以下文章