用概率模型,先随机一次看取用哪个概率,随后再随机一次。代码示例如下:
import java.util.Random;
public class HelloWorld {
public static void main(String[] args)
{
Random random = new Random();
double p1=0.7; //1~4的概率
double p=(((int)random.nextInt(10)%(10)+1)*1.0/10 < p1)?p1:(1-p1); //实际所取概率
int max=(p==p1)?4:6;
int num=(int)random.nextInt(max)%(max)+1;
System.out.print(num);
}
}