安啾

啾咪~❤

JAVA里面几个常用随机数的方法

num = Math.random() * 6;   // 0 <= num < 6 such as 0.11, 3.21, 5.99

num = Math.random() * 10;   // 0 <= num < 10 such as 2.15, 7.73, 9.99

num = (int) (Math.random() * 6);    // 0, 1, 2, 3, 4, or 5
num = (int) (Math.random() * 2);     // 0 or 1
num = (int) (Math.random() * 6) + 1;    // 1, 2, 3, 4, 5, or 6 (骰子的模型)
num = (int) (Math.random() * 2) + 1;       // 1 or 2 (投硬币的模型)
num = (int) (Math.random() * 100) + 1;     // 1 <= num <= 100
num = (int) (Math.random() * 10) - 4;      // -4 <= num <= 5

num = (int) Math.random() * 6 + 1; // always equals one

The static method named random in the Math class can be used to create a random decimal value that is greater than or equal to 0.0 and less than 1.0. 
For example, the following statement displays a random decimal value such as 0.34
                System.out.println(Math.random());

评论

© 安啾 | Powered by LOFTER