Simple gacha logic written in Python

Contents

Weight each item and perform n consecutive gachas

Parameters

The following items are prepared as parameters

#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
#Number of lottery
times = 11

Gacha processing function

import random
def gacha(item_dic, times):
	total_weight = 0
	for value in item_dic.values():
		total_weight += value

	results = []
	for i in range(times):
	    results.append(lottery(item_dic,total_weight))

    return results

def lottery(item_dic, total_weight):
	score = random.randint(1,total_weight)
	range_max = 0
	for item_key, weight in item_dic.items():
		range_max += weight
		if score <= range_max:
			return item_key			

Run gacha

item_list = gacha(item_dic, times)

I think there is a more beautiful way to write it, but for the time being.

Recommended Posts

Simple gacha logic written in Python
Gacha written in Python -BOX gacha-
Gacha written in python-Practice 1-
Gacha written in Python-Data design-
Simple regression analysis in Python
Simple IRC client in python
First simple regression analysis in Python
Simple OAuth 2 in Python (urllib + oauthlib)
Compatibility diagnosis program written in python
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
Fourier series verification code written in Python
Stress Test with Locust written in Python
A simple HTTP client implemented in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Markov chain transition probability written in Python
Gacha written in python-Rarity confirmed with bonus-
Write a simple greedy algorithm in Python
Gacha written in python-Implementation in basic data structure-
Gacha written in python-Practice 2 ・ Basics of step-up gacha-
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Set up a simple HTTPS server in Python 3
Studying Mathematics in Python: Solving Simple Probability Problems
Gacha written in python-Practice 3 ・ Addition of step-up gacha functions-