-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DimensionRandomWalk.py
More file actions
61 lines (49 loc) · 1.41 KB
/
2DimensionRandomWalk.py
File metadata and controls
61 lines (49 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 15:59:19 2022
@author: fabian
"""
import random as rd
import numpy
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default = 'browser'
rd.seed(42)
maxStep = 5
numPart = 1
NORTH = 1; SOUTH = 2; WEST = 3; EAST = 4
def singleStep():
position = numpy.zeros(shape=(2,), dtype=int)
currentStep = rd.randint(1, 4 + 1)
position[1] += numpy.where(currentStep == NORTH, 1, 0)
position[1] -= numpy.where(currentStep == SOUTH, 1, 0)
position[0] += numpy.where(currentStep == EAST, 1, 0)
position[0] -= numpy.where(currentStep == WEST, 1, 0)
return position
def walkSteps(numPart=1, numstep=100):
positionsArray = numpy.zeros(shape=(numstep+1, 2), dtype=int)
for step in numpy.arange(numstep):
positionsArray[step+1, ] = positionsArray[step, ] + singleStep()
return positionsArray
def drawWalk(positionsArray):
fig = go.Figure(data=go.Scatter(
x=positionsArray[:, 0],
y=positionsArray[:, 1],
mode='markers',
name='Randomwalk in 2 Dimensions',
marker=dict(
color=positionsArray,
size=5,
colorscale='Greens',
showscale=True),
line=dict(
color='darkblue',
width=1
)
))
fig.show()
def main():
drawWalk(walkSteps(1, 50000))
if __name__ == "__main__":
main()