Python method for generating random numbers _python

Source: Internet
Author: User
Tags shuffle
This article mainly introduces the method of generating random numbers in Python, and the friends who need them can refer to

If you have a relationship with some of the most commonly used functions in python generated random numbers versus random modules, the following article is about the relationship between Python generating random numbers and the most commonly used functions in the random module, and hopefully you will reap some of the benefits, as the following is an introduction to this article.

Random.random () is used to generate

Used to generate a random number of points within a specified range of two parameters, one of which is the upper limit and the other is the lower limit. Generates a random number if a > B

N:a <= n <= B. If a <b, then B <= N <= A.

Print Random.uniform (random.uniform) #----#18.7356606526 #12.5798298022 random.randint

Used to generate an integer within a specified range. Where parameter A is the lower bound, the parameter B is the upper bound, and Python generates a random number

Print Random.randint (#生成的随机数n): <= n <= print random.randint #print random.randint (20), ) #该语句是错误的.

The lower limit must be less than the upper limit.

Random.randrange

In the collection that increments by the specified cardinality from within the specified range, this article is part of the application that generates random numbers for Python.

Random integers:
>>> Import Random
>>> Random.randint (0,99)
21st

Randomly select even numbers between 0 and 100:
>>> Import Random
>>> random.randrange (0, 101, 2)
42

Random floating point number:
>>> Import Random
>>> Random.random ()
0.85415370477785668
>>> random.uniform (1, 10)
5.4221167969800881

Random characters:
>>> Import Random
>>> random.choice (' abcdefg%^*f ')
' d '

Select a specific number of characters in more than one character:
>>> Import Random
Random.sample (' Abcdefghij ', 3)
[' A ', ' d ', ' B ']

Select a specific number of characters to form a new string in multiple characters:
>>> Import Random
>>> Import String
>>> String.Join (Random.sample ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3)). R
Eplace ("", "" ")
' FIH '

Randomly pick a string:
>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
' Lemon '

Shuffle:
>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]

PS: Finally, we will provide you with two related online tools for your reference:

Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu

High strength random character password generator:

Http://tools.jb51.net/password/CreateStrongPassword

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.