Skip to content

Commit

Permalink
Create guessrandom.py.tns
Browse files Browse the repository at this point in the history
  • Loading branch information
duvallj authored and Vogtinator committed Sep 27, 2015
1 parent 358fdf6 commit 3ab40ba
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions nspire/examples/guessrandom.py.tns
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from nsp import readRTC

class RandomNumberGenerator:
#constructor
def __init__(self,seed=None):
self.randomints=[]
self.count=0
self.a=1664525
self.m=4294967296 # 2^32
if seed is None:
self.seed=readRTC()
else:
self.seed=seed
# rand function
def rand(self,a=None,b=None):
if self.count==0:
# append X1=a*seed mod m
self.randomints.append((self.a*self.seed)%self.m)
else:
# append Xn=a*Xn-1 mod m
self.randomints.append((self.a*self.randomints[-1])%self.m)
#increment the count
self.count+=1
if a is None and b is None:
return self.randomints[-1]/self.m
else:
return a + (self.randomints[-1]/self.m) * (b-a)

random = RandomNumberGenerator()
guess_number = int(random.rand(1,1000))
user_guess = 0

print("Welcome to the guessing game!" + "\n")

while user_guess != guess_number:
global user_guess
user_guess = int(input("Try and guess the number: "))
if user_guess > guess_number:
print("Too high" + "\n")
elif user_guess < guess_number:
print("Too low" + "\n")

print("You guessed the number!")

0 comments on commit 3ab40ba

Please sign in to comment.