Random module

Revision as of 12:55, 30 June 2020 by Icewolf10 (talk | contribs) (typo in spelling "them" lol)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The random module is used to generate pseudo-random numbers in Python.

Useful Functions

The following are functions in the random module, see the page on modules for directions on how to import them into your program.

  • random.random() returns a random floating point number between 0.0 and 1.0.
  • random.randint(a,b) returns a random integer between a and b, inclusive.
  • random.uniform(a,b) returns a random floating point number between a and b.
  • random.choice(s) returns a random element of sequence s. For example, random.choice([1,3,7]) will return either 1, 3, or 7.
  • random.shuffle(s) shuffles sequence s in place. Hence, the original s is changed and nothing is returned. If you want to preserve the original order also you need to copy the list first (myListCopy = myList[:] would work).

See Also

Invalid username
Login to AoPS