Skip to content

Instantly share code, notes, and snippets.

@yoneken
Created February 5, 2017 14:26
Show Gist options
  • Save yoneken/5274981a5ed7e5ba9340d132407cd5bd to your computer and use it in GitHub Desktop.
Save yoneken/5274981a5ed7e5ba9340d132407cd5bd to your computer and use it in GitHub Desktop.
Answer of Probabilistic Robotics Exercise 2-2
#!/usr/local/bin/python
import random
import collections
(
Sunny,
Cloudy,
Rainy,
) = range(0, 3)
Weather = ["Sunny", "Cloudy", "Rainy"]
def genWeather(today):
tomorrow = 0
rnd = random.random()
if today == Sunny:
if rnd < 0.8:
tomorrow = Sunny
else:
tomorrow = Cloudy
elif today == Cloudy:
if rnd < 0.4:
tomorrow = Sunny
elif rnd < 0.8:
tomorrow = Cloudy
else:
tomorrow = Rainy
else: # today == Rainy
if rnd < 0.2:
tomorrow = Sunny
elif rnd < 0.8:
tomorrow = Cloudy
else:
tomorrow = Rainy
return tomorrow
if __name__ == '__main__':
aryWeather = []
today = Sunny
for i in range(0, 100000):
tomorrow = genWeather(today)
aryWeather.append(tomorrow)
today = tomorrow
cnt = collections.Counter(aryWeather)
for k, v in cnt.items():
print Weather[k], v, v/100000.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment