[PYTHON] Simulation of complete gacha

About the case where all come out with the same probability n is the number of gacha items sn is the number of trials

# -*- coding: utf-8 -*-
import math
import random
import numpy as np
import time

start = time.time()

n = 100
sn = 10000

print("Complete gacha simulation(%d item)" % n)
print("Logical value: {0:.5f}".format(sum([float(n) / i for i in range(1, n+1)])))

ress = []
for j in range(sn):
    box = []
    k = 0
    i = 0
    while(i < n):
        k += 1
        r = random.randrange(n)
        if r not in box:
            i += 1
            box.append(r)
    ress.append(k)
res = np.mean(ress)
print("Simulation value: {0:.5f} [{1}Times]".format(res, sn))
print("- exec time: %d ms-" % (time.time() * 1000 - start * 1000))

Result example

Complete gacha simulation(100 items)
Logical value: 518.73775
Simulation value: 516.38000 [10000 times]
- exec time: 7640 ms-
Complete gacha simulation(10 items)
Logical value: 29.28968
Simulation value: 29.32320 [100000 times]
- exec time: 2775 ms-

Recommended Posts

Simulation of complete gacha
Simple simulation of virus infection
Simulation of the contents of the wallet
A complete understanding of Python's asynchronous programming
A complete understanding of Python's object-oriented programming
Complete everything with Jupyter ~ Introduction of nbdev ~
Gacha written in python-Practice 2 ・ Basics of step-up gacha-