Random numbers in Python

Python Methods and Functions

Operations with Randon's numbers:

1. choice () : — This function is used to generate 1 random number from a container.

2. randrange (beg, end, step) : — This function is also used to generate a random number, but within the range specified in its arguments.  This function takes 3 arguments: start number (included in generation), last number (excluded during generation) and step (to skip numbers in the range when selected).

# Python code to demonstrate how it works
# choice () and randrange ()

 
# importing "random" for random operations

import random

 
# using choice () to generate a random number from
# a list of numbers is given.

print ( " A random number from list is: " , end = " ")

print (random.choice ([ 1 , 4 , 8 , 10 , 3 ]))

 
# using randra nge () to generate in the range 20
# up to 50. The last parameter 3 is the step size to be skipped
# three numbers if selected.

print ( "A random number from range is:" , end = "")

print (random.randrange ( 20 , 50 , 3 ))

Output:

 A random number from list is: 4 A random number from range is: 41 

3. random () : — This number is used to generate a random number with a floating point less than 1 and greater than or equal to 0.

4. seed () : — This function displays a specific random number with the specified seed . All random numbers called after the seeded value will return the matched number.

# Python code to demonstrate how it works
# random () and seed ()

 
# import & quot; random & quot; for random operations

import random

 
# using random () to generate a random number
# from 0 up to 1

print ( "A random number between 0 and 1 is: " , end = " ")

print (random.random ())

 
# using seed () to fill in a random number

random.seed ( 5 )

  
# prints the displayed random number

print ( " The mapped random number with 5 is: " , end = "")

print (random.random ())

 
# using seed () to seed another random number

random.seed ( 7 )

 
# print the displayed random number

print ( "The mapped random number with 7 is:" , end = "")

print (random.random ())

  
# using seed () to seed again to 5

random.seed ( 5 )

  
# print the displayed random number

print ( "The mapped random number with 5 is:" , end = "")

print (random.random ())

  
# using seed ( ) seed again to 7

random.seed ( 7 )

  
# prints the displayed random number < / code>

print ( "The mapped random number with 7 is: " , end = " ")

print (random.random ())

Output:

 A random number between 0 and 1 is: 0.510721762520941 The mapped random number with 5 is: 0.6229016948897019 The mapped random number with 7 is: 0.32383276483316237 The mapped random number with 5 is: 0.6229016948897019 The mapped random number with 7 is: 0.32383276483316237 

5. shuffle () : — this function is used to shuffle the entire list, to randomly arrange them.

6.iform (a, b) : — this function is used to generate a random number with a floating point between the numbers given in its arguments.  It takes two arguments: a lower limit (included in generation) and an upper limit (not included in generation).

# Python- demo code
# shuffle () and uniform ()

 
# importing "random" for random operations

import random

 
# Initializing list

li = [ 1 , 4 , 5 , 10 , 2 ]

 
# Print list before shuffling

print ( "The list before shuffling is:" , end = "")

for in range ( 0 , len (li)):

print (li [i], end = "" )

print ( "" )

 
# using shuffle () to shuffle the list
random.shuffle (li)

 
# Print the list after shuffling

print ( "The list after shuffling is:"  , end = "")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

print ( " " )

  
# using uniform () to generate a random floating point number in a range
# prints a number from 5 to 10

print ( "The random floating point number between 5 and 10 is:" , end = "")

print (random.uniform ( 5 , 10 ))

Output:

 The list before shuffling is: 1 4 5 10 2 The list after shuffling is: 2 1 4 5 10 The random floating point number between 5 and 10 is: 5.183697823553464 

This article is provided by Manjit Singh. If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Tutorials