1.Math.random()方法
例子:求1到10之间的随机数
- public static void main(String[] args) {
- int n;
- for (int i = 0; i < 30; i++) {
- n = 1 + (int)(Math.random() * 10); //random()是产生【0,10)的随机数,,int是只取整数
- System.out.println("n: " + n);
- }
- }
-
-
- 2.Random的nextInt方法
-
- public static void main(String[] args) {
- int n;
- Random r;
- for (int i = 0; i < 30; i++) {
- r = new Random();
- n = r.nextInt(10)+1; //nextint(10)是产生【0,10)的整数
- System.out.println("n: " + n);
- }
- }
2989



被折叠的 条评论
为什么被折叠?



