Skip to content

Instantly share code, notes, and snippets.

@plaster
Last active March 20, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaster/692f04c1455995397700e475ed790e74 to your computer and use it in GitHub Desktop.
Save plaster/692f04c1455995397700e475ed790e74 to your computer and use it in GitHub Desktop.
1/8のガチャに3つほしいものがあるとき「コンプするまで」vs「1個でもあたるまで」の回数のシミュレーション

試行回数: 10万回

EVERY ANY
25% 9 1
50% 13 2
75% 19 3
90% 26 5
99% 43 10
99.9% 60 15
worst 99 23
avg 14.62265 2.65652
(use srfi-42)
(use math.mt-random)
(define mt (make <mersenne-twister> :seed (sys-time)))
(define (gacha)
(mt-random-integer mt 8))
(define want #b111)
(define (get having)
($ logior having $ ash 1 $ gacha))
(define (have-any? having)
($ not $ zero? $ logand want having))
(define (have-every? having)
($ = want $ logand want having))
(define (loop pred? n having)
(if (pred? having)
n
(loop pred? (+ n 1) (get having))))
(define ls1 (sort (list-ec (: x 0 100000) (loop have-every? 0 0))))
(define ls2 (sort (list-ec (: x 0 100000) (loop have-any? 0 0))))
(define (print-result label ls)
(print `(,label
"25%" ,($ list-ref ls $ floor->exact $ * 1/4 $ length ls)
"50%" ,($ list-ref ls $ floor->exact $ * 2/4 $ length ls)
"75%" ,($ list-ref ls $ floor->exact $ * 3/4 $ length ls)
"90%" ,($ list-ref ls $ floor->exact $ * 9/10 $ length ls)
"99%" ,($ list-ref ls $ floor->exact $ * 99/100 $ length ls)
"99.9%" ,($ list-ref ls $ floor->exact $ * 999/1000 $ length ls)
"worst" ,($ list-ref ls $ + -1 $ length ls)
"avg" ,(/. (apply + ls) (length ls))
)))
(print-result 'EVERY ls1)
(print-result 'ANY ls2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment