Machine Learning

How to Use Random with the Python Random Module

random editing

In our daily life, we come across several different things that are completely random. Weather is unpredictable: Sure, we can predict and forecast the weather, but only to a certain extent. Radio decay is also an interesting random process that lacks patterns and predictability. Unlike computers that are limited and operate in the way they are programmed, nature does not require any system, form, or constraints. Things happen in random and unexpected ways, and this kind of imperfection is what we sometimes need in our computers and applications, such as games.

We need randomness and unexpectedness in the games we play so that we don't get bored with planned situations and physical challenges. We also need an element of randomness in simulating real-world situations, testing algorithms, or generating random samples.

Photo by Wolfgang Hasselmann on Undercwas

In programming languages, randomness refers to introducing randomness and variation in computer output. Randomness is generated in the system by random numbers.

There are several ways to generate pseudo random numbers. Python uses Mersenne Twister randomization in its randomization module. While it is widely used as a pseudo random number generator (PRNG), the Mersenne Twister has deterministic properties, making it unsafe to obtain certain functions that require security as a priority. In the program, generating a completely random number is quite difficult, so we use the concept of generating pseudo random numbers, although they are allowed again if they are given a seed value, as can be seen beforehand.

In this article, we will explore the concept of reprogramming by using Python's random module to generate random output from our code.

Random Python module

Now let's dive deeper into the random module. First, we know that the randomness in this module is generated by the Mersenne Twister using Mersenne Primes. This built-in Python module allows us to generate randomness in our code in a variety of ways and provides flexibility while working with different data sets. Let's understand its functions with examples. You can access the official documentation for this module through the following link:

random – Generate pseudo random numbers

To use the random module, we need to make sure that import in our code first:

import random

A random floating point value between 0 and 1

The first function we will learn is to generate a random value between 0 and 1 with 0 being 0 inclusive. This can be done with random() work.

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 multiple times, each time the value will be different.

A random value to float within a certain range

We can use the uniform() The random modulo function to generate a random number in a certain range.

random_value = random.uniform(1,10)
print(random_value)

Running the above code multiple times will output numbers within the range specified in parentheses.

A random number value in a certain range

Let's say we want a random value from the dice, as it is needed in many games, we can include this feature in our code using randint() work. This function outputs a random Integer unlike the above functions which output a float value.

random_value = random.randint(1,6)
print(random_value)

Note that by using the above code snippet, 1 and 6 will include the random values ​​generated.

Photos by Aakash Dhage on UNSEPLASSH

A random value from a list of values

Next, we will see how to generate a random value from an array of values. We can do this by first defining a python array of objects, and then using a function choice() of a random number to remove a random element from that list.

For this purpose, we will first create a list and use the random module choice() A function to randomly select an item from a specified list. Let's say we have a list of our cats, and we have to choose one to give a special method. Here is how this can be done:

my_cats = ["Jerry", "Tom", "Figaro", "Bella", "Simba"]
cat_chosen = random.choice(my_cats)
print(cat_chosen)

Note that the code above is random, which means that it is not necessary that all cats will be selected (although it is very possible as we use the code), therefore, this is not the right way to choose who gets the special treatment!

In addition, we can also create a list of random selections using choices() work. This function also allows us to determine the weights of each element of the list, which means that we can increase the probability that any of the elements in the list will be randomly selected:

mylist = ["apple", "banana", "cherry", "strawberry"]
print(random.choices(mylist, weights = [2, 1, 1, 1,], k = 8))
Extract of the above code (image by Author)

In the code above, we assigned MyList as the input sequence to choice() The function, and the instruments for each item in the list next to the output list and selected items from time to time that we want. Note the number of times the fruit “apple” occurs due to its increased weight.

Random reduces the list of items

Next we will learn to exchange the items on the list. We can use the shuffle() work on a random module for this purpose.

deck = list(range(1,53))
print(deck)
random.shuffle(deck)
print(deck)
Modified output (photo by Author)
Photo by Nikil. in unchurch

A random and unique sample from the list

Let's say we want to get 5 random cards from each of the 4 players. We can't use choice() work because we want different cards from the deck, without repeating a card. We will use the sample() Work for this purpose:

deck = list(range(1,53))
cards = random.sample(deck, 5)
print(cards)
Extract of the above code (image by Author)

A random number from a certain range with a step size

This page randrange() A function can be used to periodically select a number from a specified range where start and stop values ​​and steps are defined.

random_number = random.randrange(0,10,2)
print(random_number)

The above block will generate the numbers from 0 to 8 since 10 is not inclusive and we define 2 as the step size.

The number of seeds

An interesting feature of the random module in Python is the function seed(). This seed veil is used as the starting point for random generation, and is a major factor in tracking re-dying and patterning. Whenever we use a random value to generate a random number, it actually generates it from a random seed value, but we can define the seed more realistically seed() work.

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 the defined seed value '22'. If we give the seed value '55', it will give you '0' over and over again.

Random module applications

Although there are many functions in the random module and many other uses, the above functions are some of the most used. Python's random module can be used in many ways, especially in games and real-world experiments. We can use the random module in games that involve rolling dice as tested above, in the field of money fountains, and as a random password generator. We can also simulate a Rock, paper, scissors game with a random module with specific conditions and spells!

Photo by Erik McLean at UNSEPLASPASH

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button