2023-10-27 10:13:05 +00:00
|
|
|
import random
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
def rndstr(length):
|
|
|
|
return ''.join(random.choice('0123456789abcdef') for i in range(length))
|
|
|
|
|
|
|
|
|
|
|
|
def choice(arr):
|
2023-11-27 14:20:19 +00:00
|
|
|
# randomize seed
|
2023-10-27 10:13:05 +00:00
|
|
|
return random.choice(arr)
|
|
|
|
|
|
|
|
|
|
|
|
def rndunicode():
|
2023-11-27 14:20:19 +00:00
|
|
|
# randomize seed
|
2023-10-27 10:13:05 +00:00
|
|
|
return chr(random.randint(0, 0x10FFFF))
|
|
|
|
|
|
|
|
|
|
|
|
def choice_percent(elements):
|
|
|
|
# elements is a dict of {percent: action}
|
|
|
|
# like following:
|
|
|
|
# elements = {
|
|
|
|
# 10: lambda: 'a',
|
|
|
|
# 20: lambda: 'b',
|
|
|
|
# 30: lambda: 'c',
|
|
|
|
# 40: lambda: 'd',
|
|
|
|
# }
|
|
|
|
# that means we have 10% chance to get 'a', 20% chance to get 'b', etc.
|
|
|
|
|
2023-11-27 14:20:19 +00:00
|
|
|
total_percent = sum(elements.keys())
|
2023-10-27 10:13:05 +00:00
|
|
|
|
|
|
|
# get random number
|
2023-11-27 14:20:19 +00:00
|
|
|
rnd = random.randint(1, total_percent)
|
2023-10-27 10:13:05 +00:00
|
|
|
|
|
|
|
# get action
|
2023-11-27 14:20:19 +00:00
|
|
|
cumulative_percent = 0
|
|
|
|
for percent, action in elements.items():
|
|
|
|
cumulative_percent += percent
|
|
|
|
if rnd <= cumulative_percent:
|
|
|
|
return action
|
2023-10-27 10:13:05 +00:00
|
|
|
|
|
|
|
# should not reach here
|
|
|
|
return None
|