to Randomisation
In our day-to-day life, we come across several different phenomena that are totally random. The weather is random: sure, we can forecast and predict the weather, but to a certain degree only. Radioactive decay is also an interesting random process that lacks patterns and predictability. Unlike computers that are deterministic and function the way they are programmed, nature does not require any programming, conditionals, or loops. Things happen in the most random and unpredictable ways, and this is the kind of unpredictability that we also sometimes require in our computers and applications, such as games.
We need randomness and unpredictability in the games that we play so that we do not get bored of 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 computer’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 totally random number is quite difficult, so we make use of the concept of generating the pseudo-random numbers, although they are reproducible if given a seed value, as can be seen ahead.
In this article, we will explore the concept of randomisation by employing the Python random module to generate randomness in our code’s outputs.
The Python Random Module
Now let us deep dive into the random module. Firstly, we know that randomness in 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 a variety of ways and provides flexibility while we work with different datatypes. Let us understand its functions through examples. You can access the official documentation of this module via the following link:
random — Generate pseudo-random numbers
In order to use the random module, we need to make sure to import it in our code first:
import random Random Float Value between 0 and 1
The first task we will learn is to generate a random value between 0 and 1 with 0 being non-inclusive and 1 being inclusive. This can 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. If you run the above code a number of times, each time the value will be different.
Random Float Value within a Specified Range
We can use the uniform() function of the random module in order to generate a random number in a specific range.
random_value = random.uniform(1,10)
print(random_value) Running the above code a number of times would output numbers between the range mentioned in the brackets.
Random Integer Value in a Specific Range
Suppose we want a random value from a dice, like is needed in many games, we can 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 be inclusive in the random values generated.

Random Value from a List of Values
Next, we will see how to generate a random value from a list of values. We can do this by first defining a Python list of items, and then using the function choice() of the random value to output a random item from that list.
For this purpose, we will first create a list and then use the random module’s choice() function to randomly choose an item from the said list. Suppose we have a list of our cats, and we have to choose one to give a special treat to. Here is how this can be done:
my_cats = ["Jerry", "Tom", "Figaro", "Bella", "Simba"]
cat_chosen = random.choice(my_cats)
print(cat_chosen) Notice that the above code is random, meaning it is not necessary that all the cats will be chosen (although highly probable as much as we run the code), so yeah, this is not a fair way to choose who to give the special treat to!
Moreover, we can also create a list of random choices using the choices() function. This function also allows us to decide the weights of each item of the list, meaning that we can increase the probability of any items in the list of being chosen randomly:
mylist = ["apple", "banana", "cherry", "strawberry"]
print(random.choices(mylist, weights = [2, 1, 1, 1,], k = 8)) 
In the above code, we have given mylist as the input sequence to the choice() function, as well as the weights of each item in the list alongwith how long of an output list with randomly selected items we want. Notice the number of times the fruit “apple” occurs due to its increased weight.
Random Shuffle a List of Items
Next we will learn to randomly shuffle the items in a list. We can use the shuffle() function in 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 want to get 5 random cards for each of the 4 player. We cannot use the choice() function because we want unique cards from the deck, with no card repeating. We will 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 can be used to randomly choose a number from a specific range where the start 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 8 as 10 is non-inclusive and we have defined 2 as 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 starting point for random number generation, and is a major feature to trace reproducibility and pattern. Whenever we are using the random value to generate a random number, it is actually generating it from a random seed value, but we can 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 always generate the random number ‘2’ because of a defined seed value ’22’. If we give the seed value ’55’, it will give you ‘0’ again and again.
Applications of the Random Module
Although there are more functions in the random module and many more implementations, the above functions are some of the most commonly used. Python’s random module can be used in a number of ways, mostly in games and real-world simulations. We can use the random module in games that involve rolling the dice as was explored above, in a coin toss game, and even as a random password generator. We can also simulate the Rock, Paper, Scissors game with the random module with a bit of conditionals and loops!

Source link
#Implement #Randomization #Python #Random #Module









