Sep. 26, 2012

Using the random module in Python

The random module in Python contains a number of random number generators.

Randint

#Generate integers between 1,10. The first value should be less than the second.
random.randint(1, 10)

Randrange

#Generate a randomly selected element from range(start, stop, step)
random.randrange(start, stop[, step])

Choice

#Generate a random value from the sequence sequence.
random.choice( ['red', 'black', 'green'] ).

Shuffle

#Shuffles the elements in list in place, so they are in a random order.
random.shuffle(list)

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.