Python Programming for Beginners – Random number generation in python

In this code sample today we will see random number generation in Python.

Random number generation is something which you may need in your programming assignments. There are multiple ways to generate random numbers in Python.

In this code sample today we will talk about all those different ways using which you can generate the random number.

So we will look at functions like random, randint, shuffle etc..

So to use these random functions we need to import the random module first. How do you do that ?

import random

So let us use the most basic function to generate the random number

Random number generation in python using random()

import random
x= random.random()
print (x)

result => 0.33211189847275346

Now if you do it from your end, you will get some other value. Becuase it is random number which gets generated.

If I run multiple times, I will get different values.

import random
x= random.random()
print (x)

x= random.random()
print (x)

x= random.random()
print (x)

result ==>
0.5106678055995871
0.9605032567243629
0.8082102145181462

If you see it is the same function but every time you get different result.

Random number generation in python using uniform()

Now let say you want to generate a random number between 2 numbers. For for that let us use uniform() .

import random
x= random.uniform(2,5)
print (x)
x= random.uniform(5,7)
print (x)

result ==>
3.084852997740849
6.6263773107337824

Random number generation in python using randint()

Now let us say you want to generate the random number between 2 numbers, but you want the numbers to be integer, then you can use randint().

import random
x= random.randint(2,7)
print (x)
x= random.randint(2,7)
print (x)

result >> 
6
4

Random number generation in python using choice()

Now let us say you have a list and you would like to return a random number from the list. In such case you can use choice()

import random
my_list = [1,3,6,8,9,11,15,45,44]
x=random.choice(my_list)
print (x)
x=random.choice(my_list)
print (x)
x=random.choice(my_list)
print (x)

result ==>
45
1
9

In the code above, I have called the choice() , 3 times and got three different numbers from the list.

You can actually use choice() for strings as well. So if you have a string and pass that string to choice(), it will return a random character from the string.

Do try this out.

Random number generation in python using shuffle()

Shuffle() is another good function which you have got. Shuffle() rearranges the item in a given list in a random fashion.

Let’s try that out:

import random
my_list = [1,3,6,8,9,11,15,45,44]
random.shuffle(my_list)
print (my_list)
random.shuffle(my_list)
print (my_list)
random.shuffle(my_list)
print (my_list)

result =>
[8, 3, 11, 44, 6, 9, 15, 45, 1]
[9, 8, 3, 11, 44, 45, 15, 6, 1]
[1, 3, 6, 15, 45, 8, 44, 9, 11]

So these were some common functions which I have provided as examples for you to understand this concept.

But do read the documentation for more details on this module. The link to the documentation is given below.

https://docs.python.org/3/library/random.html


You may also like: Python Programming for Beginners – Reverse a number in python

Leave a Comment

error: Content is protected !!
Share via
Copy link
Powered by Social Snap