java - Using an array of percentages to determine probability? -
after trying challenge in java make 'casino' style game, i've arrived @ point have array 10 values, each being percentage change can win. these assigned same position in array 'ids' are, eg. playerid[i] goes player percentage percentagechance[i]
at stage, i'm @ loss can go determine 'winner'. thought making random number 1-100, , whichever range lands in means person wins, i've no idea how implement that, or whether there's cleaner/more efficient technique.
you sound on right track! assuming of probabilities add 100, take random number between 1 , 100 , see lands in array.
random r = new random(); int rand = r.nextint(100) + 1; int sum = 0; for(int = 0; < 10; i++){ sum += percentchance[i]; if(sum <= rand){ //player wins } }
Comments
Post a Comment