to Randomisation
In our day-to-day life, we come across several different phenomena which might be totally random. The weather is random: sure, we are able to forecast and predict the weather, but to a certain degree only. Radioactive decay can be an interesting random process that lacks patterns and predictability. Unlike computers which might be deterministic and performance the way in which they’re programmed, nature doesn’t require any programming, conditionals, or loops. Things occur in probably the most random and unpredictable ways, and that is the form of unpredictability that we also sometimes require in our computers and applications, reminiscent of games.
We’d like randomness and unpredictability within the games that we play in order that we don’t become bored with the preprogrammed scenarios and predictable challenges. We also need the element of randomness in simulating real-world scenarios, testing algorithms, or generating sample datasets.
In programming languages, randomisation refers to introducing unpredictability and variability in the pc’s output. Randomness is generated in a program through random numbers.
There are several methods for generating pseudo-random numbers. Python uses the Mersenne Twister for randomness in its random module. While extensively used as a Pseudo-Random Number Generator (PRNG), the Mersenne Twister has deterministic properties, making it unsafe for certain tasks that requires safety as a priority. In programming, generating a very random number is sort of difficult, so we make use of the concept of generating the pseudo-random numbers, although they’re reproducible if given a seed value, as may be seen ahead.
In this text, we’ll explore the concept of randomisation by employing the Python random module to generate randomness in our code’s outputs.
The Python Random Module
Now allow us to deep dive into the random module. Firstly, we all know that randomness on this module is generated by the Mersenne Twister using Mersenne Primes. This built-in module of Python allows us to generate randomness in our code in quite a lot of ways and provides flexibility while we work with different datatypes. Allow us to understand its functions through examples. You may access the official documentation of this module via the next link:
random — Generate pseudo-random numbers
With a view to use the random module, we’d like to ensure that to import it in our code first:
import random
Random Float Value between 0 and 1
The primary task we’ll learn is to generate a random value between 0 and 1 with 0 being non-inclusive and 1 being inclusive. This may be done with the random() function.
random_value = random.random()
print(random_value)
The above code will generate a random float value between 0 and 1. When you run the above code numerous times, every time the worth will probably be different.
Random Float Value inside a Specified Range
We will use the uniform() function of the random module as a way to generate a random number in a selected range.
random_value = random.uniform(1,10)
print(random_value)
Running the above code numerous times would output numbers between the range mentioned within the brackets.
Random Integer Value in a Specific Range
Suppose we wish a random value from a dice, like is required in lots of games, we are able to include this feature in our code using the randint() function. This function outputs a random integer unlike the above functions which outputs a float value.
random_value = random.randint(1,6)
print(random_value)
Notice that by running the above piece of code, the 1 and 6 will probably be inclusive within the random values generated.

Random Value from a List of Values
Next, we’ll see find out how to generate a random value from a listing of values. We will do that by first defining a Python list of things, after which using the function alternative() of the random value to output a random item from that list.
For this purpose, we’ll first create a listing after which use the random module’s alternative() function to randomly select an item from the said list. Suppose we have now a listing of our cats, and we have now to decide on one to present a special treat to. Here is how this may be done:
my_cats = ["Jerry", "Tom", "Figaro", "Bella", "Simba"]
cat_chosen = random.alternative(my_cats)
print(cat_chosen)
Notice that the above code is random, meaning it just isn’t vital that every one the cats will probably be chosen (although highly probable as much as we run the code), so yeah, this just isn’t a good strategy to select who to present the special treat to!
Furthermore, we can even create a listing of random decisions using the decisions() function. This function also allows us to choose the weights of every item of the list, meaning that we are able to increase the probability of any items within the list of being chosen randomly:
mylist = ["apple", "banana", "cherry", "strawberry"]
print(random.decisions(mylist, weights = [2, 1, 1, 1,], k = 8))

Within the above code, we have now given mylist because the input sequence to the alternative() function, in addition to the weights of every item within the list alongwith how long of an output list with randomly chosen items we wish. Notice the variety of times the fruit “apple” occurs on account of its increased weight.
Random Shuffle a List of Items
Next we’ll learn to randomly shuffle the items in a listing. We will use the shuffle() function within the random module for this purpose.
deck = list(range(1,53))
print(deck)
random.shuffle(deck)
print(deck)


Random and Unique Sample from a List
Suppose we wish to get 5 random cards for every of the 4 player. We cannot use the alternative() function because we wish unique cards from the deck, with no card repeating. We’ll use the sample() function for this purpose:
deck = list(range(1,53))
cards = random.sample(deck, 5)
print(cards)

Random Integer from a Specific Range with Step Size
The randrange() function may be used to randomly select a number from a selected range where the beginning and stop values and the steps are defined.
random_number = random.randrange(0,10,2)
print(random_number)
The above block will produce the numbers from 0 to eight as 10 is non-inclusive and we have now defined 2 because the step size.
Seed Value
An interesting feature of the random module in Python is the function seed(). This seed value is used as a start line for random number generation, and is a significant feature to trace reproducibility and pattern. Every time we’re using the random value to generate a random number, it is definitely generating it from a random seed value, but we are able to define the seed value ourselves as well through the seed() function.
random.seed(22)
random_number = random.randrange(0,10,2)
print(random_number)
The above code will all the time generate the random number ‘2’ due to an outlined seed value ’22’. If we give the seed value ’55’, it will provide you with ‘0’ repeatedly.
Applications of the Random Module
Although there are more functions within the random module and lots of more implementations, the above functions are among the mostly used. Python’s random module may be utilized in numerous ways, mostly in games and real-world simulations. We will use the random module in games that involve rolling the dice as was explored above, in a coin toss game, and whilst a random password generator. We can even simulate the Rock, Paper, Scissors game with the random module with a little bit of conditionals and loops!

