Skip to content

Instantly share code, notes, and snippets.

@VorontsovIE
Last active December 6, 2018 00:34
Show Gist options
  • Save VorontsovIE/44214549aafd43ccbe397682bcf6a98f to your computer and use it in GitHub Desktop.
Save VorontsovIE/44214549aafd43ccbe397682bcf6a98f to your computer and use it in GitHub Desktop.
Winnie - step 2
import random
state = 'start'
# можем передать вероятности
# random_choice({'a_little': 0.1, 'y_big': 0.3, 'y_little': 0.4, 'finish': 0.2})
# или любые доли - лишь бы неотрицательные
# random_choice({'a_little': 2, 'y_big': 6, 'y_little': 8, 'finish': 4})
def random_choice(variants):
# случайное число от 0 до суммы всех долей.
random_value = random.random() * sum(variants.values())
# накапливаем долю интервала, покрытую уже рассмотренными вариантами
cumulative_portion = 0
# перебираем вариант и долю каждого интервала
for (variant, portion) in variants.items():
cumulative_portion += portion
# как только случайное число попало в интервал - возвращаем соответствующий вариант
if random_value <= cumulative_portion:
return variant
Tram = 0.5
param = 0.5
pa = 3/8
pam = 5/8
Trym = 2/7
Tyrym = 5/7
pym = 1
y_big = 0.5
a_big = 0.5
a_big_a_little = 0.75
a_big_a_big = 0.25
a_little_a_big = 1/7
a_little_a_little = 4/7
a_little_K = 2/7
y_big_y_big = 2/7
y_big_y_little = 5/7
y_little_a_little = 1/8
y_little_y_big = 4/8
y_little_y_little = 2/8
y_little_K = 1/8
while state != 'finish':
if state == 'start': # начало
next_state = random_choice({'y_big': y_big, 'a_big': 1 - y_big})
if state == 'y_big':
next_state = random_choice({'y_little': y_big_y_little, 'y_big': y_big_y_big})
phrase = random_choice({'трум': Trym, 'турум': Tyrym})
print(phrase, end = ' ')
if state == 'a_big':
next_state = random_choice({'a_big': a_big_a_little, 'a_little': a_big_a_big})
phrase = random_choice({'трам': Tram, 'парам': param})
print(phrase, end = ' ')
if state == 'y_little':
next_state = random_choice({'a_little': y_little_a_little, 'y_big': y_little_y_big, 'y_little': y_little_y_little, 'finish': y_little_K})
print('пум', end = ' ')
if state == 'a_little':
next_state = random_choice({'a_little': a_little_a_little, 'a_big': a_little_a_big, 'finish': a_little_K})
phrase = random_choice({'пам': pam, 'па': pa})
print(phrase, end = ' ')
state = next_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment