2

【趣味小题】随机漫步.

 3 years ago
source link: https://www.guofei.site/2017/07/05/randomwalk.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【趣味小题】随机漫步.

2017年07月05日

Author: Guofei

文章归类: 趣文,文章编号:


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/07/05/randomwalk.html

Edit

问题

某酒鬼随机漫步,每次等概率的走1或-1,共走1000步,每步1米。
用随机模拟方法回答问题:

  1. 有多大概率在1000步内最远走出30米或-30米,
  2. 已知最远走出30米或-30米,平均用多少步完成?

解答

这个题目主要考察数据清洗的熟练程度

import numpy as np
nwalks=5000 #5000次模拟实验
nsteps=1000 #走1000步
steps=np.random.choice([-1,1],size=(nwalks,nsteps))

walks=steps.cumsum(1)#每个时间点的所在位置

walks.max()
walks.min()

hit30_mask=(np.abs(walks)>=30).any(1)#是否走到30步

to30=np.abs(walks[hit30_mask,:])>=30
to30.argmax(1).mean()#平均用时

您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK