Movatterモバイル変換


[0]ホーム

URL:


How to Generate Random Data in Python

Generating random integers, floating point numbers, strings and bytes using random, os and secrets built-in modules in Python.
  · 6 min read · Updated may 2024 ·Python Standard Library

Confused by complex code? Let ourAI-powered Code Explainer demystify it for you. Try it out!

Randomness is found everywhere, fromCryptography toMachine Learning. Without random number generation, many things would be impossible to accomplish; in the case of cryptography, everything would be predictable and easy to break.

Arandom number generator (RNG) is a system (software or hardware component) that generates a sequence of random numbers from a true source of randomness, which can be reliable for cryptographic use. However, there arepseudo-random number generators (PRNG), which generate random numbers that look random but are actually deterministic. This means we can reproduce it if the state (or seed) of the PRNG is known.

In this tutorial, you will learn how to generate random numbers, strings, and bytes in Python using the built-inrandom module; this module implements pseudo-random number generators (which means you shouldn't use it for cryptographic use, such as key or password generation).

We will also explore thesecrets module for generating cryptographically secure random numbers suitable forpasswords, encryption keys, account authentication, and related secrets. In thefile encryption tutorial, we have used thesecrets module to derive encryption keys; make sure to check it out if you're curious.

We also usedsecrets andrandom modules togenerate random passwords.

Note that there are definitely easier ways togenerate random data, especially in Linux. However, the goal of this tutorial is to learn how to use Python for that.

RelatedHow to Use Pickle for Object Serialization in Python.

Table of contents:

Generating Random Integers

To generate random integers, we can either userandom.randint() orrandom.randrange() functions, let's see the difference:

import randomimport osimport stringimport numpy as np# generate random integer between a and b (including a and b)randint = random.randint(1, 500)print("randint:", randint)# generate random integer from rangerandrange = random.randrange(0, 500, 5)print("randrange:", randrange)

Output:

randint: 87randrange: 80

random.randint() function returns a random integer between a and b (in this case, 1 and 500) which includes a and b, in other words: a<= x <=b.

Whereasrandom.randrange() chooses a random item from that range (start=0, stop=500, step=5), which can be 0, 5, 10, 15 and so on, until 500.

Randomly Choosing Elements

Let's say we have a big list of elements, and we want to randomly select one item,random.choice() comes into the rescue:

# get a random element from this listchoice = random.choice(["hello", "hi", "welcome", "bye", "see you"])print("choice:", choice)

This will randomly choose one element from that list. Here is the output:

choice: hi

If you wish to select more than one element once, you can userandom.choices() function instead:

# get 5 random elements from 0 to 1000choices = random.choices(range(1000), k=5)print("choices:", choices)

This will select 5 elements from that range:

choices: [889, 832, 537, 110, 130]

Generating a Random Vector

For data science and machine learning use, you may want to use Numpy to generate random vectors and matrices of the desired shape. Let's generate a vector of size 20 where the values range from 0 to 1:

# get a random vector of size 20vector = np.random.random((30,))print("vector:\n", vector)

Output:

vector: [0.46724415 0.75177385 0.9832122  0.01245131 0.35557827 0.34014858 0.60991217 0.21269721 0.45915176 0.84400412 0.85336154 0.84169998 0.41310374 0.80452164 0.92334229 0.75396105 0.47011402 0.2941423 0.35021264 0.82592184 0.85020283 0.31313478 0.27498346 0.71593234 0.20603687 0.31449463 0.60271275 0.28838185 0.47468129 0.18365603]

If you don't have NumPy installed, simply install using pip install numpy.

Generating a Random Matrix

You can also generate a random matrix using the same np.random.random() function, this time we generate the values between 0 and 100:

# get a random matrix of size (3, 3) in the range [0, 100]matrix = np.random.random((3, 3)) * 100print("matrix:\n", matrix)

Output:

matrix: [[22.40423343 17.06827518 79.81260103] [74.79307033 83.86064577 92.8321372 ] [53.75464161 61.92659985 53.06586537]]

Generating Floating-Point Numbers

You can also generate floating-point numbers:

# generate a random floating point number from 0.0 <= x <= 1.0randfloat = random.random()print("randfloat between 0.0 and 1.0:", randfloat)

Output:

randfloat between 0.0 and 1.0: 0.49979177801639296

If you want to generate a float between 2 numbers, you can userandom.uniform() function:

# generate a random floating point number such that a <= x <= brandfloat = random.uniform(5, 10)print("randfloat between 5.0 and 10.0:", randfloat)

This will generate any float between 5 and 10:

randfloat between 5.0 and 10.0: 5.258643397238765

Shuffling Sequences

In order to randomly shuffle any iterable in Python, you can userandom.shuffle() function, here is an example:

l = list(range(10))print("Before shuffle:", l)random.shuffle(l)print("After shuffle:", l)

Output:

Before shuffle: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]After shuffle: [7, 8, 4, 5, 6, 9, 2, 1, 0, 3]

Generating Strings

There isn't a direct way to generate random strings in therandom module. However, we can use therandom.sample() orrandom.choices() functions to randomly select characters from a list of characters:

# generate a random stringrandstring = ''.join(random.sample(string.ascii_letters, 16))print("Random string with 16 characters:", randstring)

This will generate 16 characters (as we specified) fromstring.ascii_letters which includes allASCII characters:

Random string with 16 characters: MjIRHEGnxCSNeAiv

You can also add numbers by usingstring.ascii_letters + string.digits, or you can use only lowercase characters by usingstring.ascii_lowercase.

Cryptographically Secure Generation

As we mentioned previously, therandom module is extremely insecure for password generation or any cryptographic use. In fact, you can even userandom.seed() function to set the seed of randomness, which will generate the same sequence of numbers every time you run the program. This can be useful formachine learning or other purposes.

However, for cryptographic use, you should use thesecrets module instead; the below lines of code randomly generate different types of data securely:

# crypto-safe byte generationrandbytes_crypto = os.urandom(16)print("Random bytes for crypto use using os:", randbytes_crypto)# or use thisrandbytes_crypto = secrets.token_bytes(16)print("Random bytes for crypto use using secrets:", randbytes_crypto)# crypto-secure string generationrandstring_crypto = secrets.token_urlsafe(16)print("Random strings for crypto use:", randstring_crypto)# crypto-secure bits generationrandbits_crypto = secrets.randbits(16)print("Random 16-bits for crypto use:", randbits_crypto)

Here is the output:

Random bytes for crypto use using os: b'\xf4\xa1\xed\xb3\xef)\xfe\xd2\xe6\x86\xdb&=\xff\xf5\x9c'Random bytes for crypto use using secrets: b'\x99^\x96\x90\xe93[\x1d\x86C\xe8\xcf\x1f\xa3\x06\x86'Random strings for crypto use: RJDD-8iCEsAuDC1-N9EbQARandom 16-bits for crypto use: 2371

Learn also: How to Use Hash Algorithms in Python using hashlib.

Conclusion

In practice, you should use therandom module for statistical modeling, simulation, machine learning, and other purposes (you can also use numpy's random module to generate random arrays), to generate random data reproducible, which are significantly faster than cryptographically secure generators.

You should only use thesecrets module for cryptographic applications where data security is critical.

For further information, you can check Python's official documentation for different modules used in this tutorial:

Learn also:How to Make a Simple Math Quiz Game in Python.

Happy Generating ♥

Found the article interesting? You'll love ourPython Code Generator! Give AI a chance to do the heavy lifting for you. Check it out!

View Full Code Analyze My Code
Sharing is caring!



Read Also


How to Use Hashing Algorithms in Python using hashlib
How to Encrypt and Decrypt Files in Python
How to Send Emails in Python

Comment panel

    Got a coding query or need some guidance before you comment? Check out thisPython Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!





    Ethical Hacking with Python EBook - Topic - Top


    Join 50,000+ Python Programmers & Enthusiasts like you!



    Tags


    New Tutorials

    Popular Tutorials


    Ethical Hacking with Python EBook - Topic - Bottom






    Claim your Free Chapter!

    Download a Completely Free Ethical hacking with Python from Scratch Chapter.

    See how the book can help you build awesome hacking tools with Python!



    [8]ページ先頭

    ©2009-2025 Movatter.jp