-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathlab1_randomwalk.py
48 lines (34 loc) · 1.04 KB
/
lab1_randomwalk.py
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
'''
#------------------------------------------------------------
filename: lab1_randomwalk.py
This is an example for random walk problem using numpy ndarray
written by Jaewook Kang @ Aug 2017
#------------------------------------------------------------
'''
import numpy as np
position = 0
next_pos = 0
step = 10
randwalk = np.zeros(step)
# algo 1
# for i in range(step):
# coin = np.random.randint(2,size=1)
# randwalk[i] = position
#
# if coin == 1:
# print 'Coin is 1'
# next_pos = position + 1
# else:
# print 'Coin is 0'
# next_pos = position - 1
# position = next_pos
#
#
# algo 2
randwalk = np.zeros(step)
coin_toss_results = 2 * np.random.randint(2,size=step) - 1
randwalk = np.cumsum(coin_toss_results)
negative_value_index = np.where(randwalk < 0)
print '# My randomWalk is %s ' % randwalk
print '# Negative values in randomWalk are located at index %s' % negative_value_index
print '# Whose values are randwalk[%s] = %s' % (negative_value_index ,randwalk[negative_value_index])