directory

  • Common functions of the Python Random module are introduced
  • 2. Use Python Random module
  • Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Python randomThe module includes functions that return random numbers and can be used to simulate or any program that produces random output.

Common functions of the Python Random module are introduced

  • Random.random () – Generates a random floating point number between 0.0 (included) and 1.0 (not included);

  • Random. Uniform (A, B) – Generate a random number in the range of A ≤N≤ B. The random number type is floating point.

  • Random. Randint (a, b) — Generate a random number in the range of A ≤N≤ B. The type of the random number is integer, which is different from random.

  • Random. Sample (seq, K) — Randomly select K independent elements from SEQ sequence;

  • Random.choice (SEQ) – Randomly selects an element from the SEQ sequence, raising IndexError if SEQ is empty;

  • Random. Randrange (start, stop, step) — Returns a random number of steps from start to stop (you can use this method to return a random even or odd number), as shown in the following example:

    Returns a random even number from 0 to 100

    random.randrange(0, 101 , 2)

    Returns a random odd number from 0 to 100

    random.randrange(1, 101 , 2)

2. Use Python Random module

Python Random use cases are as follows:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python random module. Py @time :2021/3/29 07:37 @Motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" import random # Print (random. Random ()) print (random. Random ()) # print (random. Print (random. Randrange (0, 101)) print(random. Randrange (0, 101)) Print (random. Choice ([' python',' Python ',' Python tutorial '])) print (random. Sample ([10, False,)) 30, "hello", 50], k=2)) "0.9662431302672254 8.850312880563921 0 46Copy the code

3.Guess you like

  1. Python Configuration Environment
  2. Python variable
  3. Python operator.
  4. Python conditions determine if/else
  5. Python while loop
  6. Python break
  7. Python continue
  8. The Python for loop
  9. The Python string
  10. The Python list
  11. The Python tuple tuple
  12. Python dictionary dict
  13. Python conditional derivations
  14. Python list derivations
  15. Python dictionary derivations

Python random module

This article is published by the blog – Ape Say Programming Ape Say programming!