根据以往的思路,咱们还是把大问题简单化。题目要求的是随着n的变化,我们考察对于某一个特定的n,随机行走的平均步数是多少。
先考察行走一步的情况,产生一个随机数,0表示向左走,1表示向右走。看看走这一步的代码版本1:
import random
# Programmer: Python那些事
# Date: April 16, 2017
# Version 1: moves the person one step, either left or right, at random
location = 0 # tracks the person's current location
step = random.randint(0, 1) # returns 0 or 1, each with prob. 1/2
# Adjusts the random number to be either -1 or +1
if step == 0:
step = -1
location = location + step # updates location
print(location)
然后咱们将问题变得复杂一些,考虑一次随机行走,遇到n或者-n则停止,版本2如下:
import random
# Programmer: Python那些事
# Date: April 16, 2017
# Version 2: moves at random, one step at a time, left or right
# until the person reaches a barrier n or -n. Outputs the number of steps
# it took to reach the barrier
location = 0 # tracks the person's current location
n = 10 # value of the barrier
length = 0 # tracks the length of the random walk
# This moves the person until she reaches the barrier at n or -n
while abs(location) < n:
step = random.randint(0, 1) # returns 0 or 1, each with prob. 1/2
# Adjusts the random number to be either -1 or +1
if step == 0:
step = -1
location = location + step # updates location
length = length + 1
print(length)
还有两个问题需要考虑,其一是对于特定的n需要求解多次随机行走的平均步数,其二是随着n的变化,随机行走的平均步数是如何变化的。
我们需要在版本1的基础上进行循环才能解决问题1,还需要另外一个循环才能解决问题2。
那么有什么使得代码更简洁易懂的方法呢?答案肯定是有的,那就是定义函数randomWalk,接收参数n并且返回一次随机行走的步数。函数的定义方法如下:
def randomWalk( n )
其中,def是Python的关键字,randomWalk是函数名,而n是参数。函数的最后通常会有返回值,在本例中返回的就是一次随机行走的步数,即return length。
函数定义即版本3的代码如下:
import random
# Programmer: Python那些事
# Date: April 16, 2017
# Version 3: moves at random, one step at a time, left or right,
# until the person reaches a barrier n or -n. Outputs the number of steps
# it took to reach the barrier
# This function takes in the value of the barrier, simulates a random
# walk that terminates on reaching the barrier, and returns the length
# of the simulated random walk
def randomWalk(n):
location = 0 # tracks the person's current location
length = 0 # tracks the length of the random walk
# This moves the person until she reaches the barrier at n or -n
while abs(location) != n:
step = random.randint(0, 1) # returns 0 or 1, each with prob. 1/2
# Adjusts the random number to be either -1 or +1
if step == 0:
step = -1
location = location + step # updates location
length = length + 1
return length
有了randomWalk这个函数,我们很容易求解一次随机行走的步数,即:
n = input("Enter a positive integer: ")
print(randomWalk(n))
同理,对于多次随机行走平均步数的求解,代码也变得简单易读,如下代码是随机行走100次的平均步数:
# This is the main part of the program (i.e., outside the function)
n = input("Enter the value of the barrier (a positive integer): ")
sum = 0 # track the total length of all simulated random walks
counter = 0
# The simulation is repeated 100 times
while counter < 100:
sum = sum + randomWalk(n)
counter = counter + 1
print(sum/100)
然而,我们要考虑随着n的变化随机行走的平均步数,是还需要对上述代码进行循环。思考到这里,你是否有方法使得代码更简洁易懂?
对,答案还是定义函数。函数获取两个参数,即n和随机行走的次数numRepititions,函数的定义版本4的代码如下:
# Simulates random walks with barrier n as many times as specified by
# numRepititions. Returns the average length of a walk.
def manyRandomWalks(n, numRepititions):
sum = 0 # tracks the total length of all simulated random walks
counter = 0 # tracks the number of walks being performed
while counter < numRepititions:
sum = sum + randomWalk(n)
counter = counter + 1
return sum/numRepititions
最终,我们考察n=10,20,...,100的时候,随机行走100次的平均步数,主程序代码如下:
# Main loop for generating various values of the barrier
# We use barrier values 10, 20, 30, 40,..., 100
n = 10
while n <= 100:
print manyRandomWalks(n, 100)
n = n + 10
小编在电脑上运行了一下,结果是113,415,937,1501,2356,4066,4965,6358,8313,10468。当然不同的执行结果肯定不一样了,这也是随机函数的魅力了。用MATLAB软件画一下图,似乎是更是直观:
今天,你学会这种随机行走的奥秘了吗?跟你的想象有什么区别吗?配图几米的漫画是否让你想起了某段时光?学完之后,欣赏一下几米的漫画《向左走,向右走》吧。