Random类的构造方法:

public Random();//该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数

public Random(long seed);//通过制定一个种子数进行创建



方法:

public boolean nextBoolean();

public double nextDouble();//生成随机的double值,介于[0,1.0)

public int nextInt();//介于int区间-231-231-1

public int nextInt(int n);//[0,n)

Public void setSeed(long seed);

Random r=new Random();

double d1=r.nextDouble();

double d2=r.nextDouble()*5;//[0,5.0)就是将该区间扩大5倍

double d3=r.nextDouble()*1.5+1;//[1,2.5)先是生成[0,1.5)区间的随机数字在加1即可

int n1=nextInt();//生成任意整数

int n2=r.nextInt(10);//[0,10)

n2=Math.abs(r.nextInt()%10);//[0,10)


int n2=r.nextInt(n);//[0,n)

n2=Math.abs(r.nextInt()%n);

int n3=r.nextInt(11);//[0,10]

n3=Math.abs(r.nextInt()%11);//[0,10]

int n4=r.nextInt(18)-3;//[-3,15)

n4=Math.abs(r.nextInt()%18)-3;

下面的java程序演示了一些基本的操作:

package nemo;

import java.util.*;

public class lala {

public static void main(String[]args)

{

Random r=new Random();

int n2=r.nextInt(10);//[0,10)

int n3=r.nextInt(11);//[0,10]

int n1=r.nextInt(18)-2;//[-2,16)

System.out.println(n2+" "+n3+" "+n1);

boolean b1=r.nextBoolean();

boolean b2=r.nextBoolean();

System.out.println(b1+" "+b2);

double d1=r.nextDouble();//[0,1.0)

double d2=r.nextDouble()*5;//[0,5.0)

double d3=r.nextDouble()*1.5+1;//[1,2.5)

System.out.println(d1+" "+d2+" "+d3);


}

}