Rand7实现Rand10
作者:互联网
Rand7ToRand10
思路:通过放大整个随机数表示的范围(前提是等可能性),再去缩小范围找。
import java.util.Random;
public class Rand7ToRand10 {
public static void main(String[] args) {
System.out.println(rand10());
}
public static int rand10() {
int x = 50;
while(x > 40){
x = 7 * (rand7()-1) + rand7();
}
return x % 10 + 1;
}
static Random random = new Random();
public static int rand7(){
return random.nextInt(7)+1;
}
}
标签:random,实现,Random,Rand10,public,int,static,rand7,Rand7 来源: https://blog.csdn.net/weixin_43471422/article/details/120349888