Advance functions for Random number generation in Python
Random Number generation can be categoried into three categories. Part1 describes the basic type. Part2 deals with the probability distributions. Part3 covers the advanced Random number Generation.
To read the part1,part2,just follow the link.
https://rajeeva84.blogspot.com/2026/08/random-number-generation-in-python.html
https://rajeeva84.blogspot.com/2026/08/random-number-generation-in-python_02076504341.html
Advanced methods are given below…
- · Random State / Seed
- · Cryptographically Secure Randoms
1.Random State/Seed:
It uses
‘seed()’ method. This helps to initialize the generator. As a default value,it
seeds the current system time.
First method uses randint() function to print random value.
Second method uses seed() method.
Python code:
import random
# Without seed: different results each run
print("Without seed:")
for _ in range(2):
print(random.randint(1, 8))
# With seed: same results every run
random.seed(42)
print("\nWith seed (40):")
for _ in range(2):
print(random.randint(1, 8))
Output:
Without seed:
6
8
With seed (40):
2
1
Next advance method covers the secret key generations.
Cryptographically Secure Randoms:
It has
some built-in modules for generate secret values like passwords and tokens.
Built-in package: secrets
Let us write the python code as follows.
First, import the secrets module and string.
This program covers 6 different methods to generate secret
values.
First, it generates random integer within the range given.
Second method creates random integer in 32 bits of size.
Third develops a string randomly from the given list.
Fourth generates a random token in hexadecimal.
Fifth method secretly creates URL value.
Finally, it creates a random password with the combination
of strings, integers and ascii values.
Python Code:
import secrets
import string
def secure_random_eg():
try:
# 1. let us
Secure random integer within the range
rand_int =
secrets.randbelow(50)
print(f"Secure random integer (0-50): {rand_int}")
# 2. Secure
random integer between two numbers
rand_range =
secrets.randbits(32)
print(f"Secure 32-bit random number: {rand_range}")
# 3. Secure
random string in a list
vehicles =
['audi', 'bmw', 'maruthi','honda','tesla']
rand_choice =
secrets.choice(vehicles)
print(f"Secure random choice: {rand_choice}")
# 4. Secure
random token in hexadecimal
token_hex =
secrets.token_hex(16)
print(f"Secure random hex token: {token_hex}")
# 5. let us
create random token for URL
token_url =
secrets.token_urlsafe(16)
print(f"Secure random URL-safe token: {token_url}")
# 6. random
password generator
sample =
string.punctuation + string.digits + string.ascii_letters
password =
''.join(secrets.choice(sample) for _ in range(12))
print(f"Secure random password: {password}")
except Exception
as e:
print(f"Error generating secure randoms: {e}")
if __name__ == "__main__":
secure_random_eg()
Output:
Secure random integer (0-50): 22
Secure 32-bit random number: 164742466
Secure random choice: audi
Secure random hex token: 83600591f6ec5c62e9aafac09b06653c
Secure random URL-safe token: H8880pWsUXSTMgMyKqBLIw
Secure random password: F4}V|vQ%_#M_
These are the various methods to generate random numbers in
advance manner. Hope, this code is useful to you. Keep Coding!!!
Comments
Post a Comment