Following are the few explanatory illustrations using different python modules, on how to generate random integers? Consider the scenario of generating the random numbers between 0 and 9 (both inclusive).
Syntax:
random.randrange(stop) random.randrange(start, stop, step)
Code:
>>> import random >>> for i in range(10): ... print(random.randrange(10)) ... 2 2 2 0 8 8 5 6 6 3
random.randint(a,b)
>>> import random >>> for i in range(10): ... print(random.randint(0,10)) ... 1 6 7 5 8 9 6 2 3 9 >>>
By using this method, we can generate cryptographically strong random numbers.
>>> from secrets import randbelow >>> for i in range(10): ... print(randbelow(10)) ... 6 5 2 0 7 2 0 1 2 6 >>>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Using randrange
Syntax:
random.randrange(stop) random.randrange(start, stop, step)Code:
Using randint
Syntax:
Code:
Using secrets
By using this method, we can generate cryptographically strong random numbers.
need an explanation for this answer? contact us directly to get an explanation for this answer