得到0-100的随机数
random()
package com.itlwc;
public class Test {
public static void main(String[] args) {
int a = 100;
int num = (int) (Math.random() * a);
System.out.println(num);
}
}
Random
package com.itlwc;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random r = new Random();
// nextInt()返回一个伪随机数
int num = r.nextInt(100);
System.out.println(num);
}
}
得到100-200的随机数
random()
public class Test {
public static void main(String[] args) {
int a = 100;
int b = 200;
int num = (int) ((Math.random() * (b-a)) + 100);
System.out.println(num);
}
}
Random
package com.itlwc;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random r = new Random();
int a = 100;
int b = 200;
// nextInt() 返回一个伪随机数
int num = r.nextInt(b-a)+a;
System.out.println(num);
}
}