Python Random Module

 


Random Module

Python has a built-in module that you can use to make random numbers.

The random module has a set of methods:

Method

Description

seed()

Initialize the random number generator

getstate()

Returns the current internal state of the random number generator

setstate()

Restores the internal state of the random number generator

getrandbits()

Returns a number representing the random bits

randrange()

Returns a random number between the given range

randint()

Returns a random number between the given range

choice()

Returns a random element from the given sequence

choices()

Returns a list with a random selection from the given sequence

shuffle()

Takes a sequence and returns the sequence in a random order

sample()

Returns a given sample of a sequence

random()

Returns a random float number between 0 and 1

uniform()

Returns a random float number between two given parameters

triangular()

Returns a random float number between two given parameters, you can also set a mode parameter to specify the midpoint between the two other parameters

betavariate()

Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics)

expovariate()

Returns a random float number based on the Exponential distribution (used in statistics)

gammavariate()

Returns a random float number based on the Gamma distribution (used in statistics)

gauss()

Returns a random float number based on the Gaussian distribution (used in probability theories)

lognormvariate()

Returns a random float number based on a log-normal distribution (used in probability theories)

normalvariate()

Returns a random float number based on the normal distribution (used in probability theories)

vonmisesvariate()

Returns a random float number based on the von Mises distribution (used in directional statistics)

paretovariate()

Returns a random float number based on the Pareto distribution (used in probability theories)

weibullvariate()

Returns a random float number based on the Weibull distribution (used in statistics)

 

random() Method

The random() method returns a random floating number between 0 and 1.

Example

Return random number between 0.0 and 1.0:

import random
print(random.random())

Output:

0.73

randint() Method

The randint() method returns an integer number selected element from the specified range.

Syntax

random.randint(start, stop)

Parameter Values

Parameter

Description

start

Required. An integer specifying at which position to start.

stop

Required. An integer specifying at which position to end.

Example

Return a number between 3 and 9 (both included):

import random
print(random.randint(3,9))

Output:

4

Note: This method is an alias for randrange(start, stop+1)

randrange() Method

The randrange() method returns a randomly selected element from the specified range.

Syntax

random.randrange(start, stop, step)

Parameter

Description

start

Optional. An integer specifying at which position to start.
Default 0

stop

Required. An integer specifying at which position to end.

step

Optional. An integer specifying the incrementation.
Default 1

Example

Return a number between 3 and 9:

import random
print(random.randrange(39))

#returns a number between 3 (included) and 9 (not included)

Output:

5

choice() Method

The choice() method returns a randomly selected element from the specified sequence.

The sequence can be a string, a range, a list, a tuple or any other kind of sequence.

Syntax

random.choice(sequence)

Parameter Values

Parameter

Description

sequence

Required. A sequence like a list, a tuple, a range of numbers etc.

Example

Return a random element from a list:

import random
myl = [
"apple""banana""cherry"]
print(random.choice(myl))

Output:

Apple

Example

Return a random character from a string:

import random
x = 
"WELCOME"
print(random.choice(x))

Output:

C

shuffle() Method

The shuffle() method takes a sequence, like a list, and reorganize the order of the items.

Note: This method changes the original list, it does not return a new list.

Syntax

random.shuffle(sequence)

Parameter Values

Parameter

Description

sequence

Required. A sequence.

function

Deprecated since Python 3.9. Removed in Python 3.11.
Optional. The name of a function that returns a number between 0.0 and 1.0.
If not specified, the function random() will be used

 

Example

Shuffle a list (reorganize the order of the list items):

import random
mylist = [
"apple""banana""cherry"]
random.shuffle(mylist)
print(mylist)

Output:

["apple", "cherry", "banana"]

Example

This example uses the function parameter, which is deprecated since Python 3.9 and removed in Python 3.11.

You can define your own function to weigh or specify the result.

If the function returns the same number each time, the result will be in the same order each time:

import random
def myfunction():
  
return 0.1
mylist = [
"apple""banana""cherry"]
random.shuffle(mylist, myfunction)
print(mylist)

Output:

["banana", "cherry", "apple"]

uniform() Method

The uniform() method returns a random floating number between the two specified numbers (both included).

Syntax

random.uniform(a, b)

Parameter Values

Parameter

Description

a

Required. A number specifying the lowest possible outcome

b

Required. A number specifying the highest possible outcome

Example

Return a random number between, and included, 20 and 60:

import random
print(random.uniform(2060))

Output:

53.29

<<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Microsoft Word top 180 keyboard shortcuts

Microsoft Word top 180 keyboard shortcuts Download shortcut keys File: MS Word File - Click here MS Excel File - Click here PDF File - Click...

Blogger द्वारा संचालित.