Summary of Python random number generation method

Source: Internet
Author: User

The method of generating random numbers in Python is mainly concentrated in the random sub-module of the NumPy module:
Import NumPy as NP
(1) generates a random floating-point number with a range of 0-1:np.random.random ()
(2) generate a random floating-point number within a specified range: Np.random.uniform (A, B)
Here, yes. A A, a, a parameter refers to a range

(3) Generate random integers in the specified range: Np.random.randint (A, B)

(4) Generate any number in the specified range: Print Randrange (A, B)

Generates any n incrementing sequence in a specified range: Print Randrange (a,b,n)

(5) Random acquisition of an element

Random.choice (Sequence)

 random.choice("Hello world!")#输出随机字符 random.choice(["Hello","world"])#输出随机的单词

(6) Generate a random array:
Rand (D0,D1...,DN): Generates n-dimensional arrays, parameters are the number of each dimension, the elements are [0,1] floating-point numbers, subject to uniform distribution
Randint (Low,hight, (Shape)): The preceding parameter represents the range, and the following parameter represents the shape
Randn (D0,D1...DN): Similar to the first one, but this obeys the normal distribution

(6) There are two common ways to disrupt elements
List=[' A ', ' B ', ' C '] random.shuffle (list)

(7) Randomly select n elements from a sequence without altering the original sequence
A= "123456" b=[1,2,3,4,5,6] c=[' A ', ' B ', ' C ', ' d ', ' e ']
Np.random.sample (a,3)
Np.random.sample (b,3)
Np.random.sample (c,3)

This article is written in reference to other articles. Ha

Summary of Python random number generation method

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.