Random Numbers in PythonA random number is a number that is generated by a process that is inherently unpredictable, meaning that the sequence of numbers produced does not follow a pattern that can be easily determined by an observer. Random numbers are important in many areas of computing, such as cryptography, simulations, and games, as they can be used to generate unique and unpredictable results.
Generating a Single Random NumberUsing random.randint() You can generate a single random number in Python by using the random module. The randint generates a random integer between two specified numbers, inclusive of both endpoints. Following is an example that generates a random integer between 1 and 100:
import random minimum = 1 maximum = 100 random_number = random.randint(minimum, maximum) print(random_number) //Output: 63Using random.random() Here's another example that output a random floating-point number between 0 and 1:
import random random_number = random.random() print(random_number) //Output:0.8977624344043958
Generate 10 random numbers between 1 and 100You can generate a list of 10 random numbers between 1 and 100 in Python using a for loop and the randint function from the random module:
import random minimum = 1 maximum = 100 random_numbers =  for i in range(10): random_numbers.append(random.randint(minimum, maximum)) print(random_numbers) //Output: [54, 68, 31, 2, 44, 85, 9, 14, 46, 77]Above code generates a list of 10 random integers between minimum and maximum and appends each number to the list random_numbers. The for loop is used to repeat the process 10 times. The resulting list of random numbers is then printed.
How to pick a random number from a listYou can pick a random number from a list in Python by using the choice function from the random module. choice returns a randomly selected element from a non-empty sequence.
import random numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] random_number = random.choice(numbers) print(random_number) //Output: 6Above code first creates a list of numbers, and then uses random.choice to select a random number from the list. The resulting random number is then printed.
Generate Array of Random Integer ValuesYou can generate an array of random integer values in Python using the randint function from the numpy library. The numpy library provides a fast and efficient way to perform mathematical operations on arrays. Following is an example that generates an array of 5 random integers between 1 and 100:
import numpy as np minimum = 1 maximum = 100 number_of_random_numbers = 5 random_numbers = np.random.randint(minimum, maximum + 1, size=number_of_random_numbers) print(random_numbers) //Output: [97 64 55 21 86]This code uses the randint function from the numpy library to generate an array of number_of_random_numbers random integers between minimum and maximum (inclusive), and then prints the resulting array of random numbers. Note that in numpy arrays, the size argument specifies the shape of the array, so the argument size=number_of_random_numbers means that the resulting array will have number_of_random_numbers elements.
Generating random integers using sample()You can generate a sample of random integers within a given range in Python using the sample function from the random module. The sample function generates a random sample from a specified population without replacement. Following is an example that generates a sample of 5 random integers between 1 and 100:
import random minimum = 1 maximum = 100 population = range(minimum, maximum+1) sample_size = 5 random_sample = random.sample(population, sample_size) print(random_sample) //Output: [69, 49, 47, 63, 55]Above code first creates a range of numbers between minimum and maximum (including maximum), which serves as the population. The random.sample function is then used to generate a sample of sample_size elements from the population. The resulting sample of random integers is then printed.
Generating a random number using randrange()
You can generate a random number from a specified range in Python using the randrange function from the random module. randrange generates a random integer from a specified range, inclusive of the start value but exclusive of the stop value. Here's an example that generates a random integer between 1 and 100:
import random start = 1 stop = 101 random_number = random.randrange(start, stop) print(random_number) //Output: 49This code generates a random integer between start and stop (exclusive of stop) using the randrange function, and then prints the result. Note that you can use randrange to generate a random integer from a range that is equivalent to a list.
Generating a randomly shuffled list using shuffle()You can generate a randomly shuffled list in Python using the shuffle function from the random module. The shuffle function randomly reorders the elements of a list in place. Here's an example that shuffles a list of numbers:
import random numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] random.shuffle(numbers) print(numbers) //Output: [7, 1, 5, 8, 6, 4, 10, 3, 2, 9]This code first creates a list of numbers, and then uses the shuffle function to randomly reorder the elements of the list. The resulting shuffled list is then printed. Note that the shuffle function operates in place, meaning that it shuffles the elements of the original list, rather than creating a new shuffled list.
Generating random number sequence seed()You can generate a sequence of random numbers with a specific seed in Python using the seed function from the random module. The seed function sets the starting value for the pseudorandom number generator, which is used by the random module to generate sequences of random numbers. By providing a specific seed, you can generate a reproducible sequence of random numbers. Following is an example that generates a sequence of 10 random numbers with a specific seed:
import random seed_value = 42 random.seed(seed_value) for i in range(10): print(random.random())
//Output: 0.6394267984578837 0.025010755222666936 0.27502931836911926 0.22321073814882275 0.7364712141640124 0.6766994874229113 0.8921795677048454 0.08693883262941615 0.4219218196852704 0.029797219438070344This code first sets the seed value using the seed function, and then generates a sequence of 10 random numbers using the random function. The same sequence of random numbers will be generated each time the code is run with the same seed value. If you run the code multiple times without setting the seed, you will get a different sequence of random numbers each time.
- How to use Date and Time in Python
- Python Exception Handling
- How to pause execution of program in Python
- How do I parse XML in Python?
- How to read and write a CSV files with Python
- Threads and Threading in Python
- Python Multithreaded Programming
- Python range() function
- How to Convert a Python String to int
- Python filter() Function
- Difference between range() and xrange() in Python
- How to print without newline in Python?
- How to remove spaces from a string in Python
- How to get the current time in Python
- Slicing in Python
- Create a nested directory without exception | Python
- How to measure time taken between lines of code in python?
- How to concatenate two lists in Python
- How to Find the Mode in a List Using Python
- Difference Between Static and Class Methods in Python?