Home » Python Random Module

Python Random Module

Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification
Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription

seed() Initialize the random number generator

import random

random.seed(10)
print(random.random()) 

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

import random

print(random.getrandbits(4))

randrange() Returns a random number between the given range

import random

print(random.randrange(1, 10)) 

randint() Returns a random number between the given range

import random

print(random.randint(1, 10)) 

choice() Returns a random element from the given sequence

import random

mycountries = ["France", "Germany", "Spain"]

print(random.choice(mycountries)) 

choices() Returns a list with a random selection from the given sequence

shuffle() Takes a sequence and returns the sequence in a random order

import random

mycountries = ["France", "Germany", "Spain"]
random.shuffle(mycountries )

print(mycountries ) 

sample() Returns a given sample of a sequence

random() Returns a random float number between 0 and 1

import random

print(random.random())

uniform() Returns a random float number between two given parameters

import random

print(random.uniform(10, 50)) 

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

expovariate() Returns a random float number based on the Exponential distribution

gammavariate() Returns a random float number based on the Gamma distribution

gauss() Returns a random float number based on the Gaussian distribution

lognormvariate() Returns a random float number based on a log-normal distribution

normalvariate() Returns a random float number based on the normal distribution

vonmisesvariate() Returns a random float number based on the von Mises distribution

paretovariate() Returns a random float number based on the Pareto distribution

weibullvariate() Returns a random float number based on the Weibull distribution

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More