連続変化する重みづけを考慮したランダム数値を取得するプログラムを作った。
発想:一様乱数で得られた数値から対応する重みづけ関数の要素を呼び出し(0から1の値)
もう一度一様乱数を呼び出し、呼び出された数字が参照した重みづけ関数の値を上回らなかったら
抽選をやり直す
== ここから ==
;;
pro weighted_random
;; thres_f = 重みづけを表現した関数 ;;
a=0.5
b=1.-0.5
n_thres = 360l
thres_f = cos(2*!dpi*dindgen(n_thres)/n_thres)*a + b
n_test = 3600l
result = dblarr(n_test)
for i=0, n_test-1, 1 do begin
jump1:
;; 抽選 ;;
tmp_ran = long(randomu(seed)*n_elements(thres_f))
;; 選択個所の閾値チェックをクリアできるか試す ;;
dice = randomu(seed)
;; diceが指定確率以上の値を取ったら再度抽選 ;;
if dice gt thres_f[long(tmp_ran)] then begin
goto, jump1
endif
;; 条件を満たし、確率チェックをクリアしたものをカウントする ;;
result[i] = tmp_ran
endfor
;; 分布をプロット ;;
plot,histogram(result),psym=10,xs=1
;; 分布を可視化 ;;
y = randomu(seed,n_test)*0.2 - 0.1
wset,1
plot,result,y, psym=1, yr=[-0.2,0.2],xs=1
wset,0
return
end