Skip to content

Instantly share code, notes, and snippets.

@VorontsovIE
Created December 6, 2018 00:37
Show Gist options
  • Save VorontsovIE/2106371c4ee128a1edb6fee9bb968cf6 to your computer and use it in GitHub Desktop.
Save VorontsovIE/2106371c4ee128a1edb6fee9bb968cf6 to your computer and use it in GitHub Desktop.
Winnie - step 3
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
while state != 'finish':
if state == 'start': # начало
next_state = random_choice({'y_big': 1, 'a_big': 1})
if state == 'y_big':
next_state = random_choice({'y_little': 5, 'y_big': 2})
phrase = random_choice({'трум': 2, 'турум': 5})
print(phrase, end = ' ')
if state == 'a_big':
next_state = random_choice({'a_big': 3, 'a_little': 1})
phrase = random_choice({'трам': 0.5, 'парам': 0.5})
print(phrase, end = ' ')
if state == 'y_little':
next_state = random_choice({'a_little': 1, 'y_big': 4, 'y_little': 2, 'finish': 1})
print('пум', end = ' ')
if state == 'a_little':
next_state = random_choice({'a_little': 4, 'a_big': 1, 'finish': 2})
phrase = random_choice({'пам': 5, 'па': 3})
print(phrase, end = ' ')
state = next_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment