public class RandomTest{ public static void main(String[] args){ int i=Math.random();//random()会自动产生一个0.0-1.0的双精度随机数 System.out.println(i);//输出 i=Math.random()*1000;//产生0-1000的双精度随机数 System.out.println(i); int b=(int)(Math.random()*1000);//产生0-1000的整数随机数 System.out.println(b); } } |
import java.util.random
public class RandomTest{
public static void main(String[] args){
Random random=new Random();//创建random对象 int intNumber=random.nextInt();//获取一个整型数 float floatNumber=random.nextFloat();//获取一个浮点数(0-1) double doubleNumber=random.nextDouble();//获取双精度数(0-1) boolean booleanNumber=random.nextBoolean();//获取boolean数 System.out.println("intNumber:"+intNumber); System.out.println("floatNumber:"+floatNumber); System.out.println("doubleNumber:"+doubleNumber); System.out.println("booleanNumber:"+booleanNumber); } } |
Random random=new Random(100); |
import java.util.random
public class RandomTest{
public static void main(String[] args){
Random random1=new Random(100); Random random2=new Random(100); for(int i=0;i<5;i++){ System.out.print(random1.nextInt()+"\t"); System.out.println(random2.nextInt()+"\t"); System.out.println("---------------------------------"); } } } |