我发现了这个密码:
import numpy as np
import matplotlib.pyplot as plt
# We create 1000 realizations with 200 steps each
n_stories = 1000
t_max = 500
t = np.arange(t_max)
# Steps can be -1 or 1 (note that randint excludes the upper limit)
steps = 2 * np.random.randint(0, 1 + 1, (n_stories, t_max)) - 1
# The time evolution of the position is obtained by successively
# summing up individual steps. This is done for each of the
# realizations, i.e. along axis 1.
positions = np.cumsum(steps, axis=1)
# Determine the time evolution of the mean square distance.
sq_distance = positions**2
mean_sq_distance = np.mean(sq_distance, axis=0)
# Plot the distance d from the origin as a function of time and
# compare with the theoretically expected result where d(t)
# grows as a square root of time t.
plt.figure(figsize=(10, 7))
plt.plot(t, np.sqrt(mean_sq_distance), 'g.', t, np.sqrt(t), 'y-')
plt.xlabel(r"$t$")
plt.tight_layout()
plt.show()
而不是只做步骤-1或1,我想做的步骤遵循一个标准正态分布。。。当我插入np.随机.正态(0,11000)而不是np.随机.随机(……)它不起作用。在
顺便说一句,我对Python还很陌生
致以衷心的感谢和良好的问候
您正在输入一个数字作为
np.random.normal
的第三个参数,因此您将得到一个1d数组,而不是2d数组,请参见documentation。试试这个:相关问题 更多 >
编程相关推荐