random module
The standard library module for pseudo-random numbers and random choices, such as randrange(10) or choice(colours).
In more detail
randrange(a, b) picks a whole number from a up to but not including b, while randint(a, b) includes b. choice picks one item, shuffle mixes a list in place, sample picks several without repeats and choices picks with repeats. Setting a seed repeats the same sequence, which is useful for tests.
Example
from random import randrange, choice
randrange(1, 7)Related terms
- random seed — The starting value of a pseudo-random number generator; the same seed always produces the same sequence of numbers.
- pseudo-random number — A number produced by a formula that looks random but is completely determined by where the sequence started.
- secrets module — The standard library module for generating random values that are safe to use as passwords, tokens and other secrets.
Taught in
- What Python Is, And Where It Came From — Hands-on Python: Complete Video Course & Book
- Where Python Lives: PATH And Versions — Hands-on Python: Complete Video Course & Book
- Repeat Loops And Successive Modification — Hands-on Python: Complete Video Course & Book
- Colours, Custom And Random — Hands-on Python: Complete Video Course & Book
- Modules And The Import System — Hands-on Python: Complete Video Course & Book
- Dates, Times And Random — Hands-on Python: Complete Video Course & Book
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep