c# - Container that returns random value based on a chance/wight -
how best go making container class returns random values based on weight entered during adding of element?
ideally should usable this:
var randomcontainer = new randomcontainer<ithing>(); randomcontainer.add(new coolthing(), 80); randomcontainer.add(new awesomething(), 20); ithing thing = randomcontainer.get();
so thing have 80% chance of beingness coolthing , 20% chance of beingness awesomething.
but algorithm shouldn't require weights add together 100, should possible too:
var randomcontainer = new randomcontainer<ithing>(); randomcontainer.add(new coolthing(), 398); randomcontainer.add(new awesomething(), 485); randomcontainer.add(new specialthing(), 1); ithing thing = randomcontainer.get();
any ideas how implement such algorithm? members/properties should class have? create sense implement icollection?
that algorithm should pretty simple:
select random value between 1 , sum of weights. value indicate object return.
example sec sample:
values 1 398 homecomingcoolthing
. values 399 883 (= 398 + 485) homecoming awesomething
value 884 homecoming specialthing
c# algorithm random containers
No comments:
Post a Comment