Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python random module random

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report