File: guessrandom.py.tns

package info (click to toggle)
giac 1.6.0.41%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 64,540 kB
  • sloc: cpp: 351,842; ansic: 105,138; python: 30,545; javascript: 8,675; yacc: 2,690; lex: 2,449; makefile: 1,243; sh: 579; perl: 314; lisp: 216; asm: 62; java: 41; sed: 16; csh: 7; pascal: 6
file content (43 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (4)
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
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!")