generated at
乱数網羅率

1~100までの乱数を何回も生成し、1~100まですべてが出現するまでの回数を数える
何度も試してその平均を求める
サイコロでやった方がわかりやすいかな?

クーポン収集問題というらしい
6(1/1+1/2+...+1/6)らしい
なんでやろ...

moura.js
N = 6 // サイコロ //N = 100 MAX = N * 10 HEIGHT = MAX + 100 function setup(){ createCanvas(400,HEIGHT) histogram = [] for(i=0;i<MAX;i++) histogram[i] = 0 strokeWeight(0) total = 0 trials = 0 } function draw(){ visited = {} for(count=0,coverage=0;coverage<N;count++){ i = Math.floor(random(N)) if(!visited[i]) coverage++ visited[i] = true } total += count trials += 1 background('#ffc') fill('black') text(`今回: ${count}`,10,30) text(`平均: ${total/trials}`,70,30) histogram[count] += 1 fill('blue') for(i=N;i<MAX;i++){ rect(10,i-N+40,histogram[i]*2,1) } }

moura.rb
trials = 10000 count = 0 (0...trials).each { visited = {} total = 0 while true count += 1 i = rand(100) if !visited[i] then visited[i] = true total += 1 break if total == 100 end end } puts count / trials


出典: 増井