# Type this command in bash to get a copy of this file:
# wget http://cs.whitman.edu/~davisj/cs/167/2016F/exmpls/randomness.py

import random
import time
import turtle

def winner(votes1, votes2):
  # Code from textbook exercise 5.1.8
  if votes1 > votes2:
    print('Candidate one wins!')
  elif votes1 < votes2:
    print('Candidate two wins!')
  else:
    print('There was a tie.')

def loaded():
  """
  Simulates a roll of a dice loaded to come up 1 or 6 half the time
  """
  r = random.random()
  if r < 0.25:
    return 1
  elif r < 0.5:
    return 6
  elif r < 0.625:
    return 2
  elif r < 0.75:
    return 3
  elif r < 0.875:
    return 4
  else:
    return 5

def printLoadedHistogram(n):
  ones = 0
  twos = 0
  threes = 0
  fours = 0
  fives = 0
  sixes = 0
  for count in range(n):
    value = loaded()
    if value is 1:
      ones += 1
    elif value is 2:
      twos += 1
    elif value is 3:
      threes += 1
    elif value is 4:
      fours += 1
    elif value is 5:
      fives += 1
    else:
      sixes += 1
  print(ones*"=")
  print(twos*"=")
  print(threes*"=")
  print(fours*"=")
  print(fives*"=")
  print(sixes*"=")
  
def lehmer(r,m,a):
  """
  r = seed or previous random number
  m = a prime number
  a = any integer between 1 and m-1
  """
  return (a*r) % m

def lehmerSequence(r,m,a,n):
  """n is the number of trials"""
  results = []		# initialization (to an empty list)
  for i in range(n):
    r = lehmer(r,m,a)
    results.append(r)	# update (appends to the list)
  return results	

def parkMillerSequence(length, seed):
  r = seed
  m = 2**31 - 1
  a = 16807
  randList = []
  for index in range(length):
    r = lehmer(r,m,a)
    randList.append(r)
  return randList

def scaledParkMillerSequence(length, seed, stop):
  r = seed
  m = 2**31 - 1
  a = 16807
  randList = []
  for index in range(length):
    r = lehmer(r,m,a)
    randList.append(r % stop)
  return randList

def testRandom(n):
  """
  Draw n random points selected using random.random().
  """
  tortoise = turtle.Turtle()
  screen = tortoise.getscreen()
  screen.setworldcoordinates(0,0,1,1)
  screen.tracer(100)    # Only draw every 100 updates
  tortoise.up()         # Don't draw lines
  tortoise.speed(0)     # Go as fast as possible
  for i in range(n):    # Draw the n random points
    x = random.random()
    y = random.random()
    tortoise.goto(x,y)
    tortoise.dot()
  screen.update()       # Draw any remaining dots
  screen.exitonclick()

def testLehmer(n):
  """
  Draw n random points selected using the Lehmer random number generator.
  """
  y = int(time.time()*1000) # Need a seed value
  m = 2**31 - 1         # Good values for the Lehmer function
  a = 16807
  tortoise = turtle.Turtle()
  screen = tortoise.getscreen()
  screen.setworldcoordinates(0,0,1,1)
  screen.tracer(100)    # Only draw every 100 updates
  tortoise.up()         # Don't draw lines
  tortoise.speed(0)     # Go as fast as possible
  for i in range(n):    # Draw the n random points
    # Generate two random numbers using the lehmer function.
    x = lehmer(y,m,a)
    y = lehmer(x,m,a)
    tortoise.goto(x/m,y/m)
    tortoise.dot()
  screen.update()       # Draw any remaining dots
  screen.exitonclick()

def loaded7or11():
  """
  Rolls two loaded dice and returns True if the results add up 
  to 7 or 11.
  Verbose version.
  """
  roll1 = loaded()
  roll2 = loaded()
  sum = roll1 + roll2
  #print(sum) # For debugging purposes
  if sum==7:
    return True
  if sum==11:
    return True
  return False

def loaded7or11():
  """
  Rolls two loaded dice and returns True if the results add up 
  to 7 or 11.
  Concise version.
  """
  sum = loaded() + loaded()
  #print(sum) # For debugging purposes
  return sum == 7 or sum == 11

def monitor(temperature):
  return 97.9 < temperature < 99.3

def main():
  #print(parkMillerSequence(100, int(1000*time.time())))
  #print(scaledParkMillerSequence(100, int(1000*time.time()), 2))
  #testRandom(10000)
  #testLehmer(10000)
  #for i in range(10):
  #  print(loaded7or11())
  print(monitor(96.6))

main()
