Random Number problem-it is known that there is a Random7 () function, returns a random natural number from 1 to 7, so that the Random7 () is used to construct a random 1 ~ Random10 ~ 10. random7random10
Math. random () randomly generates the float number between (0, 1), and Random7 randomly generates an integer between [1, 7], and uses Random7 to construct Random10:
1. Generate a: a is the result of two Random7 requests, that is, a = (Random7 ()-1) * 7 + Random7 ()-1;
2. judge whether a is less than 40. If it is less than 40, return a/4 + 1, that is, the final result; otherwise, continue to execute Random10;
Public class B
{
Public static int Random7 (){
Return (int) (Math. random () * 7 + 1 );
}
Public static int Random10 (){
Int a = 49;
While (a> = 40 ){
A = (Random7 ()-1) * 7 + Random7 ()-1;
}
Return a/4 + 1;
}
Public static void main (String [] args)
{
For (int I = 0; I <50; I ++ ){
System. out. println (Random10 ());
}
}
}