鱼C论坛

 找回密码
 立即注册
查看: 1116|回复: 7

[已解决]随机漫步 不产生随机的点 咋回事哦

[复制链接]
发表于 2018-9-18 16:10:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from random import choice

class RandomWalk():
       
       
        def __init__(self,num_points=5000):
               
                self.num_points = num_points
               
               
                self.x_values = [0]
                self.y_values = [0]
               
               
        def fill_walk(self):
               
                while len(self.x_values) < self.num_points:
                       
                        x_direction = choice([1,-1])
                        x_distance = choice([0,1,2,3,4])
                        x_step = x_direction * x_distance
                       
                        y_direction = choice([1,-1])
                        y_distance = choice([0,1,2,3,4])
                        y_step = x_direction * x_distance
                       
                       
                        if x_step == 0 or y_step == 0:
                                continue
                               
                        next_x = self.x_values[-1] + x_step
                        next_y = self.y_values[-1] + y_step
                       
                       
                        self.x_values.append(next_x)
                        self.y_values.append(next_y)





import matplotlib.pyplot as plt

from random_walk import RandomWalk

rw = RandomWalk()
rw.fill_walk()

plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show()


代码和书上的都是一样的啊  有什么问题吗
最佳答案
2018-9-18 19:47:40
你 y 坐标打错了,不是xdiatance,应该是Y的,,,x轴y轴都一样还怎么漫步。。。。肯定是一条直线啦

总是产生这种图

总是产生这种图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-18 16:19:20 From FishC Mobile | 显示全部楼层
没问题吧,越跑越远不是吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-18 16:24:51 | 显示全部楼层
claws0n 发表于 2018-9-18 16:19
没问题吧,越跑越远不是吗?

想产生随机的点   每次的方向和步长都是随机产生的,不应该都集中自一条线上啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 16:44:58 From FishC Mobile | 显示全部楼层
小甲鱼的大鲨鱼 发表于 2018-9-18 16:24
想产生随机的点   每次的方向和步长都是随机产生的,不应该都集中自一条线上啊

范围300,取 5000 点,是不是太多了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 17:17:20 | 显示全部楼层
正常应该是类似矩形的区域,中间浓度大,你查查函数类的定义,看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 19:47:40 | 显示全部楼层    本楼为最佳答案   
你 y 坐标打错了,不是xdiatance,应该是Y的,,,x轴y轴都一样还怎么漫步。。。。肯定是一条直线啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 19:49:08 | 显示全部楼层
看我的图,多帅哦
Figure_1.png

  1. from random import choice
  2. import matplotlib.pyplot as plt

  3. class RandomWalk():
  4.         
  5.         
  6.         def __init__(self,num_points=5000):
  7.                
  8.                 self.num_points = num_points
  9.                
  10.                
  11.                 self.x_values = [0]
  12.                 self.y_values = [0]
  13.                
  14.                
  15.         def fill_walk(self):
  16.                
  17.                 while len(self.x_values) < self.num_points:
  18.                         
  19.                         x_direction = choice([1,-1])
  20.                         x_distance = choice([0,1,2,3,4])
  21.                         x_step = x_direction * x_distance
  22.                         
  23.                         y_direction = choice([1,-1])
  24.                         y_distance = choice([0,1,2,3,4])
  25.                         y_step = y_direction * y_distance
  26.                         
  27.                         
  28.                         if x_step == 0 or y_step == 0:
  29.                                 continue
  30.                                 
  31.                         next_x = self.x_values[-1] + x_step
  32.                         next_y = self.y_values[-1] + y_step
  33.                         
  34.                         
  35.                         self.x_values.append(next_x)
  36.                         self.y_values.append(next_y)








  37. rw = RandomWalk()
  38. rw.fill_walk()

  39. plt.scatter(rw.x_values,rw.y_values,s=15)
  40. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-19 21:52:37 | 显示全部楼层
RIXO 发表于 2018-9-18 19:47
你 y 坐标打错了,不是xdiatance,应该是Y的,,,x轴y轴都一样还怎么漫步。。。。肯定是一条直线啦

对啊 才发现 谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 12:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表