forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
358fdf6
commit 3ab40ba
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |