1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

マルコフ連鎖でしかのこのこのここしたんたん

Posted at

こんな動画があったのでPythonで実際にマルコフ連鎖してみる。

ソースコード

shikanokonokonokokoshitantan.py
import random

# 遷移
transitions = {
    '': ['', ''],
    '': [''],
    '': [''],
    '': ['', '', ''],
    '': [''],
    '': [' ', ''],
}

# 重み
weights = {
    '': [0.5, 0.5],
    '': [0.5, 0.25, 0.25],
    '': [0.5, 0.5],
}

# 初期状態
state = ''
result = [state]

# 空白が出たら終了
while True:
    next_state = random.choices(
        transitions[state],
        weights = weights.get(state, [1])
    )[0]
    result.append(next_state)
    state = next_state
    if state == ' ':
        break

# 結果出力
print(''.join(result))

状態遷移図

おわりに

MITライセンスにするので面白いことに使ってほしい。

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?