admin管理员组

文章数量:1794759

常识

1.random

代码语言:python代码运行次数:0运行复制
import matplotlib.pyplot as plt
import random

position = 0
walk = [position]
steps = 1000
for i in range(steps):
    step = 1 if random.randint(0,1) else -1
    position +=step
    walk.append(position)
plt.plot(walk)

2.np.random

代码语言:python代码运行次数:0运行复制
import numpy as np
import matplotlib.pyplot as plt

nsteps = 1000
draws = np.random.randint(0,2,size = nsteps)
steps = np.where(draws>0,1,-1)
# np.where(condition, x, y):是三元表达式 x if condition else y的向量化
walk = steps.cumsum()
plt.plot(walk)

本文标签: 常识