How To Generate Random Numbers In Linux?


Generating random numbers can seem odd. But it is very important for security. Insecurity field whatever best cipher or algorithm you use if you can’t generate random numbers it is useless from point of view of security. So how can we generate random numbers in a Linux environment?

Generate With urandom File

urandom is a device which resides under the /dev . We can read this file which will output some binary values. We can use cat or similar command to print or redirect into a file.

$ cat /dev/urandom
Generate With urandom File
Generate With urandom File

Generate Random Numbers With Openssl

The second option is to use OpenSSL which is a core security library for a lot of application. OpenSSL rand 256 commands will generate 256 bytes of random data with the binary format. to generate more readable format like hex use -hex option  rand -hex 256.

$ openssl rand -hex 256
Generate With Openssl
Generate With Openssl

Generate Random Numbers With Python

The third option is using python random library. But this library generates random numbers rather than random data. We will use random module and random() function like below. This will generate a random number between 1 and 0.

import random
random.random()
Generate With Python
Generate With Python

LEARN MORE  Python Standard Library

Leave a Comment