パイソンの質問
私は1つのランダムステップの関数を持っています:
def random_step():
""" chooses a random step (-1 or 1) and returns it.
inputs: none! However, make sure to use parens when calling it.
For example: ramdom_step()
"""
return random.choice([-1, 1])
そして、私が書いているこの関数でそれを呼び出す必要があります:
rw_outcome( start, numsteps )
、次の 2 つの入力を受け取ります。
start
、夢遊病者の開始位置を表す整数numsteps
、開始位置から取るランダムなステップの数を表す正の整数
numsteps
への呼び出しを使用してサイズが決定されるランダムなステップで構成されるランダム ウォークをシミュレートする必要がありますがrandom_step()
、同じ開始位置を返し続けます。
print('start is', start) で返されるものの例:
>>> rw_outcome(40, 4)
start is 40
start is 41
start is 42
start is 41
start is 42
42
私がこれまでに持っているもの:
def rw_outcome(start, numsteps):
print('start is', start)
if start + (numsteps*random_step()) == 0:
return 0
else:
return rw_outcome(start,numsteps+1)
再帰で書くことは可能ですか?