python - Random int without importing 'random' -


is there way let program select random number between 1 , 1,000 without importing 'random'?

help appreciated.

based on random source code:

def randint(a, b):     "return random integer in range [a, b], including both end points."     return + randbelow(b - + 1)  def randbelow(n):     "return random int in range [0,n).  raises valueerror if n<=0."     if n <= 0:        raise valueerror     k = n.bit_length()     numbytes = (k + 7) // 8     while true:         r = int.from_bytes(random_bytes(numbytes), 'big')         r >>= numbytes * 8 - k         if r < n:             return r  def random_bytes(n):     "return n random bytes"     open('/dev/urandom', 'rb') file:         return file.read(n) 

example:

print(randint(1, 1000)) 

you implement random_bytes() using prng.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -