Python Programming - The Random Module

You can learn about Functions in Python Programs with Outputs helped you to understand the language better.

Python Programming – The Random Module

Modules are files that contain code meant to be used in other programs. These modules usually group together a collection of programming related to one another. The random module contains functions related to generating random numbers and producing random results. Random numbers have many applications in science and computer programming, especially when there are significant uncertainties in a phenomenon of interest. The first line of code in the program introduces the import statement. The statement allows you to import or load modules. In the case of the random module:
.import random

random ( )

The random module contains functions that return random numbers, which can be useful for simulations or any program that generates random out¬put. Python has a module random for generating random numbers. The numbers are generated randomly. random( ) tend to be equally distributed between 0 and 1. The random function can be used to generate pseudorandom floating-point values. It requires no parameters and returns values uniformly distributed between 0 and 1 (including 0, but excluding 1). The following example shows the usage of the random ( ) function:

  • The random ( ) function is part of the random package which returns a floating-point value.

Example
Demo of random ( ) function.

import random # This will import random module
print ( random . random ( ) )
print ( random . random ( ) )
print ( random . random ( ) )RUN
>>>
0.03843263415457954
0.5650010796211149
0.27553471708860566
>>>

randint ( )

If you want a random integer, you can use the ran- dint function. Randint accepts two parameters: a lowest and the highest number and returns an integer between lowest and highest (including both). This function is not accessible directly so you need to import a random module and then you need to call this function using the random static object. Generate integers between 1,5. The first value should be less than the second. This will output either 1, 2,3,4, or 5.

Example
Demo of randint ( ) function.

import random # This will import random module
print ( random . randint ( 0 , 5 ) )
print ( andom . randint ( -4 , 20 ) )RUN
>>>
3
13
>>>
>>>
4
8
>>>

uniform ( )

The function random. uniform, when supplied with two numerical parameters a and b, returns a random (uniformly distributed) real number.

Example
Demo of uniform ( ) function.

import random # This will import random module
print ( random . uniform ( 0 , 5))
print(random.uniform(-4, 20))RUN
>>>
3.9638451355692887
-3.1520760763005606
>>>

randrange ( )

randrange([start], stop, [step]) returns a random number from range(start, stop, step). The function random.randrange is the standard function for generating a random integer in the range you would get by calling range with the same arguments. For example, to get a random number in the range from 1 to 10 (inclusive), you would use randrange(l,ll) (or, alternatively, randrange(10)+l), and if you want a random odd positive integer lower than 20, you would use randrange(l,20,2).

Example
Demo of randrange ( ) function.

import random # This will import random module
print ( random . randrange ( 6 ) )
print ( random . randrange ( 1 , 20 , 2 ) )

RUN
>>>
1
1
>>>
>>>
4
19
>>>

Example
Demo of randrange ( ) function.

Let’s ask the user how many dice to throw, and how many sides each one should have. The dice-throwing mechanism is implemented with randrange and a for loop.

from random import randrange
num = int ( input ( ‘ How many dice? ‘ ) )
sides = int ( input ( ‘ How many sides per dice? ‘ ) )
sum = 0
for i in range(num):
sum += randrange ( sides ) + 1
print ( ‘ The result is ‘, sum )RUN
>>>
How many dice? 3
How many sides per dice? 6
The result is 11
>>>

Let’s Try

Observe the following Python code and find out, which out of the given options i) to iv) are the expected correct output(s). Also, assign the maximum and minimum value that can be assigned to the variable ‘Go’.

i. 100 $$ 75 $$ 10
ii. 75 $$ 10$$ 125 $$
iii. 75$$ 10$$
iv. 10 $$125 $$100

import random
x = [ 100 , 75 , 10 , 125 ]
Go = random . randint ( 0 , 3 )
for i in range ( Go ) :
print ( x [ i ] , “$$” , )
>>>
100 $$ 75 $$ 10 $$
>>>

The minimum value that can be assigned to Go is 0.
The maximum value that can be assigned to Go is 3.

Leave a Reply

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