How generate random number according to some statics table .like need number between 1 to 6. Chances of getting 1 is 30%, chances of getting 2 is 25% and chances of getting 6 is 10% etc..
Asked
Active
Viewed 78 times
-3
-
or https://stackoverflow.com/questions/44763015/java-how-do-i-generate-a-random-number-that-is-weighted-towards-certain-number – OH GOD SPIDERS Feb 12 '18 at 13:41
-
Generate random number from 1 to 100, then use condition, if from 1 to 10 then choose 6 ect. – ulou Feb 12 '18 at 13:41
1 Answers
0
There is no such a method to do this. You must create it.For example:
static int generateRandom(){
Random rnd = new Random();
int number = rnd.nextInt(100);
if(number<30){
//30% chance to return 1
return 1;
}else if(number<30+25){
//25% chance to return 2
return 2;
}else if(number<30+25+10){
//10% chance to return 6
return 6;
}else{
//45% chance to return 3
return 3;
}
}

xyz
- 812
- 9
- 18