2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで書くシンプルなガチャロジック

Last updated at Posted at 2020-05-03

#内容
アイテム別に重み付けをしてn連ガチャを行います

##パラメータ
パラメータとして以下の物を用意

# 抽選対象のアイテムIDと重みのdictionary
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
# 抽選回数
times = 11

##ガチャ処理の関数

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			

ガチャを実行

item_list = gacha(item_dic, times)

もう少し綺麗な書き方があると思いますが、とりあえず。

2
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?