Random numbers are needed in most studies in information technology such as:
- Simulation
- Game Development
- Machine Learning, AI, Image recognition
- Cryptography
- Code testing and many more.
Random module in Python has all the functions. You need to import Random to work with random functions in Python. Using this module you can generate random numbers and sequences. However, the random number generated are pseudo-random numbers, random numbers that are deterministic. In order to generate truly random numbers one has to resort to physical systems like noise, radioactive decay or weather related phenomenon.
Here is a brief description of the functions:
1. random.random():
Generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive).
2. random.uniform(a, b):
Generates a random floating-point number between a and b, inclusive of a but exclusive of b.
3. random.randint(a, b):
Generates a random integer between a and b, inclusive of both a and b.
4. random.randrange(start, stop[, step]):
Generates a random integer from the range [start, stop), with an optional step value.
5. random.choice(seq):
Returns a random element from the sequence seq.
6. random.shuffle(x):
Shuffles the elements of the sequence x in place.
7. random.sample(population, k):
Returns a k length list of unique elements chosen from the population sequence or set.
Sequence is a general form of data structure that stores a collection of items in a specific order. It can be of different data types. In simple terms, an ordered list of same type of data is a sequence.
Here is a better example of a sequence and a list:
Tuples (Immutable Sequences):
- Ordered collection of elements.
- Elements cannot be changed after creation.
Often used to represent fixed data structures.
Lists (Mutable Sequences):
- Ordered collection of elements.
- Elements can be modified, added, or removed.
More flexible for dynamic data structures.
===============
The random.shuffle(mylist) function modifies the list mylist in place and does not return a new list. This means that you need to have an existing list that you want to shuffle. The function directly changes the order of elements in the original list.
===============
Here is an example of the usage of the randum() function in PyCharm, Randoms.py. If you want use the code to run, remove space between each function shown below.
import random
# Generate a random float between 0 and 1
random_float = random.random()
print("Random Float: ", random_float)
# Generate a random integer between 1 and 10 (inclusive)
random_integer = random.randint(1, 25)
print("Random Integer: ", random_integer)
random_number_with_step = random.randrange(5, 67, 3)
# Generates a random integer from the range [start, stop), with an optional step value
print("Random Number with step: ", random_number_with_step)
random_choice_seq = random.choice([1, 200, 300, 412, 516])
# Returns a random element from the sequence (list as argument)seq.
print("Random Choice Sequence (list): ", random_choice_seq)
random_choice_seq=random.choice('A thing of beauty is a joy forever')
# Returns a random letter from the sequence (string as argument)seq
print("Random Choice Sequence (string): ", random_choice_seq)
random_shuffle_of_the_list=random.shuffle([1, 100, 1000, 10000])
#Shuffles the elements of the sequence x in place
print("Random Shuffle list in place: ", random_shuffle_of_the_list)
#random_Sample=random.sample(population, k)
#Returns a k length list of unique elements chosen from the population sequence or set.
# unlike random.shuffle, random.sample can accept a list variable or use list elements as argument
# Original list
my_list = [1, 100, 1000, 10000]
# Get a sample of 2 elements from the list
random_Sample_Population_k = random.sample(my_list, 2)
# Print a sampled list
print("Random_Sample_Population_k: ", random_Sample_Population_k)
------------------------------------------------------
Being random, each time you run the result will be different except the shuffle list
Random Float: 0.7630766884865164
Random Integer: 1
Random Number with step: 26
Random Choice Sequence (list): 412
Random Choice Sequence (string): a
Random Shuffle list in place: None
Random_Sample_Population_k: [1, 1000]