Python random Library


title: Random

Random

Using random library allows to randomize integers and lists.

Randomize integers between range of ‘a’ and ‘b’:

Simply replace a and b variables. This will print a random number between 1 and 10.

import random a = 1 b = 10 print(random.randint(a,b))

or just use the numbers instead of variables:

import random print(random.randint(1,10))

Randomize items in a list

Using choice() fuction to random select a list item. These can be a mix of different types, such as integers, booleans and strings. This will print random item from the list.

import random list = ["hello", 2, False, "world", 1337] print(random.choice(list))

More about random library for Python 3 at python docs.

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.