Created
September 23, 2017 01:47
-
-
Save hyrious/8fcf8e2f8f2b07a7c8f8af50c6e434aa to your computer and use it in GitHub Desktop.
linear probability draw card
This file contains hidden or 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
def draw b, c, d, q = rand | |
pc = 2.0 / ((d + 1) * (c - b + 1)) | |
s = pb = d * pc | |
dp = (d - 1) * pc / (c - b) | |
while s < q | |
s += pb -= dp | |
b += 1 | |
end | |
b | |
end | |
p Array.new(20) { draw 1, 200, 100 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment