Changing probability in java -
this question has answer here:
- weighted randomness in java 7 answers
i have been trying write program simple fighting simulator , task have ask user percentage of low, medium, , high attacks in fighting round.
so example, user inputs low = 50%, medium = 30%, , high = 20%. question stems here. how make in 1 round program print low 50% of time, medium 30% of time, , high 20% of time?
without giving exact java code, here need do:
- create set of intervals. in case,
[0, 0.5>
,[0.5, 0.8>
;[0.8, 1.0>
. - generate random number between 0 , 1
- see interval falls in. fall first interval 50% of time, second interval 30% of time, , third interval 20% of time
you can figure out here?
afterthought: in case, values user inputs add 100%. in general may not true; compute "apparent intervals" first, , scale them maximum of 1. let's user entered 50,30,30% (can't math...). intervals be
0, .5 .5, .8 .8, 1.1
and have divide 1.1
right intervals. or multiply random number 1.1
.
Comments
Post a Comment