プログラミングのお題スレ
■ このスレッドは過去ログ倉庫に格納されています
0369デフォルトの名無しさん
2013/12/06(金) 17:15:55.19| probDist thresholds bag dice randGen |
probDist := #(1 2 1 2 1 2).
probDist := probDist / probDist sum.
thresholds := probDist inject: #(0) into: [:acc :prob | acc, {acc last + prob}].
randGen := Random new.
dice := [ | rnd | rnd := randGen next. thresholds findLast: [:thre | thre < rnd]].
bag := Bag new.
1e5 timesRepeat: [bag add: dice value].
^bag sortedElements "=> {1->10906 . 2->22107 . 3->11161 . 4->22417 . 5->11119 . 6->22290} "
■ このスレッドは過去ログ倉庫に格納されています