c# - Random number generation based on different factors -
this question has answer here:
- random weighted choice 8 answers
i want write random number generation algorithm. not want numbers random. want them dependent on number of different factors. factors, mean variables value between 0 1.
so factors -
bounce // 0.56 // should afffect outcome 10% // inversely proportional outcome spin // 0.71 // should afffect outcome 20% // inversely proportional outcome light // 0.99 // should afffect outcome 5% // directly proportional outcome crowd support // 1.00 // should afffect outcome 5% // directly proportional outcome pressure // 0.89 // should afffect outcome 10% // inversely proportional outcome experience // 0.76 // should afffect outcome 10% // directly proportional outcome skill // 0.56 // should afffect outcome 40% // directly proportional outcome
now based on these factors, want generate random number between 0-6 or wicket.
i writing algorithm simulate cricket game.
what approach take write such algorithm?
idea calculate random coefficient factors individually. lower distribution range on resulting characteristic.
int max = 6; var rand = new random(); //to make code cleaner, returns 0..1 double func<double> next = () => rand.nextdouble(); double result = - bounce * max * 0.1 * next() - spin * max * 0.2 * next() + light * max * 0.05 * next() //0.1 + crowdsupport * max * 0.05 * next() //0.1 - pressure * max * 0.1 * next() + experience * max * 0.1 * next() //0.2 + skill * max * 0.4 * next(); //0.6 //calculated based on weights , negative\positive character //result in range 0 , 6
another idea calculate positive , negative factors separately , them apply random coeficient of them, divide 2 , add half of max. you'll random distribution 0 6.
double negativefactor = bounce * max * 0.25 + spin * max * 0.5 + pressure * max * 0.25; double positivefactor = light * max * 0.1 + crowdsupport * max * 0.1 + experience * max * 0.2 + skill * max * 0.6; double result = max / 2 + positivefactor * max * next() / 2 - negativefactor * max * next() / 2;
as lasse v. karlsen correctly noted, need pick weights positive factors in such way, sum 1. distribution include 6 max value, if negative factors zero. example of such factors gave in comments source code.
for negative factors, allow them decrease resulting value 40%
. if want include 0 result, need make such coeficient, sum 1, examples on comments
Comments
Post a Comment