Skip to content

Instantly share code, notes, and snippets.

@plaster
Last active March 18, 2018 16:18
Show Gist options
  • Save plaster/238e3cb4e43727bd1564998a2f267247 to your computer and use it in GitHub Desktop.
Save plaster/238e3cb4e43727bd1564998a2f267247 to your computer and use it in GitHub Desktop.
チョコエッグ16種のうち残り4種をコンプしたいんだけどいくつ買ったらどのくらいの確率でコンプできるか
(use srfi-27)
(use util.match)
(define (play-out n N x)
;; x times trial
;; n species to get
;; N species supplied
(let1 C (- (ash 1 n) 1) ;; 1111... [n times]
(let loop [[ c 0 ]
[ x x ]
]
(cond [ (= C (logand C c)) #t ]
[ (zero? x) #f ]
[else
(loop (logior c (ash 1 (random-integer N)))
(- x 1))
]
))))
(define (win-rate p s)
;; (p): -> win(#t) | lose(#f)
;; s: sampling count
(let loop [[ x s ]
[ w 0 ]
]
(if (zero? x)
(/ w s)
(loop (- x 1)
(+ w (if (p) 1 0))))))
(define main
(match-lambda
[ (_ n N x0 x1 s)
(let [[ n (string->number n) ]
[ N (string->number N) ]
[ x0 (string->number x0) ]
[ x1 (string->number x1) ]
[ s (string->number s) ]
]
(for-each
(^(x)
(format #t "~a ~a~%"
x
(exact->inexact (win-rate (cut play-out n N x) s))
)
)
(iota (+ x1 1 (- x0)) x0))
(exit 0)
)]))
ル╹ヮ╹ルɔ cymbal:~/work/gacha
% gosh gacha.scm 4 16 20 60 10000
20 0.2572
21 0.2802
22 0.3053
23 0.3349
24 0.3664
25 0.3979
26 0.42
27 0.4507
28 0.4718
29 0.5049
30 0.5192
31 0.5533
32 0.583
33 0.5943
34 0.6225
35 0.6377
36 0.6603
37 0.6774
38 0.691
39 0.7093
40 0.7282
41 0.7422
42 0.7546
43 0.7733
44 0.779
45 0.7855
46 0.8115
47 0.8185
48 0.8196
49 0.8389
50 0.8466
51 0.8491
52 0.866
53 0.8725
54 0.8854
55 0.8934
56 0.8963
57 0.9036
58 0.9125
59 0.9174
60 0.9164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment