Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python random module random

Shulou Source: shulou.com Published: 2022-06-02 15:19:13 10月15日 Update

This article mainly explains "how to use python random module random". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the python random module random!

I. introduction of common functions of random module

Random.random ()-generates a random floating point number from 0.0 (inclusive) to 1.0 (exclusive)

Random.uniform (a, b)-generates a random number in the range a ≤ N ≤ b. The random number type is floating point.

Random.randint (a, b)-generates a random number in the range of a ≤ N ≤ b. The type of random number is shaping, which is different from random.uniform (a, b).

Random.randrange (start, stop, step)-returns a random number with steps of step from the beginning of start to the end of stop (you can use this method to return random even or odd numbers), example:

# return random even numbers random.randrange (0,101,2) # return random odd numbers random.randrange (1,101,2)

Random.sample (seq, k)-k independent elements are randomly selected from the seq sequence.

Random.choice (seq)-randomly extracts an element from the seq sequence and throws an IndexError exception if seq is empty.

II. Use of random module

#! usr/bin/env python#-*-coding:utf-8 _ *-"@ Author:jb51@Blog (personal blog address): xuebuyuan.com@WeChat Official Account (official Wechat account): we @ Github:www.github.com@File:python_random.py@Time:2019/11/06 21:25@Motto: without accumulating steps, you cannot reach thousands of miles; without accumulating small streams, you cannot become rivers and seas. The highlights of program life need to be accumulated unremittingly!" Import random# generation range is 0.0 ≤ x

Tags: Generation module random number element point range extraction even content odd sequence method type learning independent practical deeper wonderful persistent non-accumulative step Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno MariaDB Shulou Information Docker