Python | os.getrandom() method Last Updated : 18 Oct, 2019 Comments Improve Suggest changes Like Article Like Report OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getrandom() method is used to generate a string of size random bytes suitable for cryptographic use or we can say this method generates a string containing random characters. It can also be used to seed user-space random number generators. It can return less 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 : Python3 # Python program to explain os.getrandom() method # importing os module import os # Declaring size size = 5 # Using os.getrandom() method # Using os.GRND_NONBLOCK flag result = os.getrandom(size, os.GRND_NONBLOCK) # Print the random bytes string # Output will be different everytime print(result) Output: b'5\n\xe0\x98\x15' Example #2 : Python3 # Python program to explain os.getrandom() method # importing os module import os # Declaring size size = 5 # Using os.getrandom() method # Using os.GRND_RANDOM flag result = os.getrandom(size, os.GRND_RANDOM) # Print the random bytes string # Output will be different everytime print(result) Output: b'\xce\xc8\xf3\x95%' Comment More infoAdvertise with us Next Article Python | os.getrandom() method R Rajnis09 Follow Improve Article Tags : Python python-os-module Practice Tags : python Similar Reads Python | os.getenv() method OS module in Python provides functions for interacting with the operating system. OS comes under Python OS env standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.getenv() method in Python OS env returns the value of the os environment 4 min read Python | os.getsid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getcwd() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getpid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getpid() method in Python is used to get the process ID of the current process 2 min read Python | os.getenvb() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getenvb() method in Python is bytes version of os.getenv() method. This method 3 min read Python | os.getcwdb() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f 1 min read Python | os.getppid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getppid() method in Python is used to get the parent process ID of the current 2 min read Python | os.getpgid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.getlogin() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.getlogin() method in Python is used to get the name of the user logged in on t 1 min read Python | os.ctermid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.ctermid() method in Python is used to get the filename corresponding to the co 1 min read Like