+

Python | os.getrandom () method

os.getrandom () is used to generate a string of random bytes of size suitable for cryptographic use, or we can say that this method generates a string containing random characters. It can also be used to populate random number generators in user space. It may return fewer bytes than requested.

Syntax: os.getrandom (size, flag)

Parameter:
size: It is the size of string random bytes
flag: It is a bit mask that can contain zero or more flags ORed together ... Flags are os.GRND_RANDOM and GRND_NONBLOCK.

Return Value: This method returns a string which represents random bytes suitable for cryptographic use.

Flags —

os.GRND_NONBLOCK: If this flag is set then getrandom () does not block but instead immediately raises BlockingIOError if no random bytes are available to read.

os.GRND_RANDOM: If this bit is set then random bytes are drawn from the / dev / random pool.

Example # 1:

Exit :

 b`5 xe0x98x15` 

Example # 2:

# Python program to explain the os.getrandom () method

 
# import of the os module

import os 

  # Size declaration

size = 5

 
# Using the os. getrandom ()
# Using the os.GRND_NONBLOCK flag

result = os.getrandom (size, os.GRND_NONBLOCK) 

 
# Output a string of random bytes
# Output will be different every time

print (result) 

# Python program to explain the os.getrandom () method

 
# import of the os module

import os 

 
# Size declaration

size = 5

 
# Using the os.getrandom () method
# Using the os.GRND_RANDOM flag

result = os.getrandom (size, os.GRND_RANDOM) 

 
# Output a string of random bytes
# Output will be different every time

print (result) 

Exit:

 b`xcexc8xf3x95% `
Get Solution for free from DataCamp guru