Last active
December 21, 2022 02:02
-
-
Save naohaq/698ea503deaf13f0200e1c425e1e849f to your computer and use it in GitHub Desktop.
Monte Carlo sim for voting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- mode: ruby; coding: utf-8-unix -*- | |
def rsample(n,m) | |
l = n+m-1 | |
bins = Array.new(l,0) | |
(0..(m-2)).each{|k| bins[k]=1} | |
(0..(l-2)).each do |k| | |
j = rand(l-k) + k | |
t = bins[k] | |
bins[k] = bins[j] | |
bins[j] = t | |
end | |
result = Array.new(m,0) | |
i = 0 | |
c = 0 | |
bins.each do |b| | |
if b != 0 then | |
result[i] = c | |
i += 1 | |
c = 0 | |
else | |
c += 1 | |
end | |
end | |
result[-1] = c | |
result | |
end | |
n = 200 | |
m = 63 | |
q = 16 | |
N = 10000 | |
c = 0 | |
(1..N).each do | |
bins = rsample(n,m) | |
if bins.any?{|b| b>q} then | |
c += 1 | |
end | |
end | |
printf "%d / %d\n", c, N |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment