Python-Common modules-random module

Source: Internet
Author: User

The random module in Python is primarily used to generate random numbers

1 #!/usr/bin/env python2 #Coding:utf-83 4 ImportRandom One #1, Random.random () returns a random number that produces a value between 0-1 AA =random.random () - Print('A:', a) # a:0.6166193118806547 -  the #2, Random.uniform (start,end) returns a random number between [Start,end] ([] Here is the range of mathematics, which means that can be equal to) -b = Random.uniform (5, 10)#here if start is larger than end, auto will turn, keep small in front - Print('B:', b) # b:9.592194042992439 -  + #3, Random.randint (start,end) returns a random integer between [start,end] -c = Random.randint (5, 10)#here if start is larger than end, auto will turn, keep small in front + Print('C:', c) # C:5 A  at #4, Random.randrange (start,stop,step) returns a random integer between [start,stop] tangent interval step -D = Random.randrange (10,30,2) - Print('D:', D) # d:14 -E = Random.randrange (10)#If only one value is given, it represents any positive integer from 0 to the value ([0,10]) - Print('e:', E) # E:6 -  in #5. Random.choice (SEQ) returns a value randomly from the ordered sequence (list, tuple, str) SEQ -L = ['a', 1,'b', [2,3],{'K1':'v1','K2':'v2'}] tos ='Hello_world' +t = (1,'m', 4,[1,2,3],{'K1':'v1','K2':'v2'}) -f =Random.choice (L) theg =Random.choice (s) *h =Random.choice (t) $ Print('F:', f) # f:aPanax Notoginseng Print('g:', G) # G:e - Print('h:', h) # h:1 the  + #6. Random.shuffle (x) randomly disrupts the position of elements in the sequence list, returning none Ai =random.shuffle (L) the Print('I:', i) # I:none + Print('L:', L) # L: [1, [2, 3], ' B ', ' a ', {' K1 ': ' v1 ', ' K2 ': ' V2 '}] -  $ #7, Random.sample (seq,k) randomly obtains fragments of the specified length (not contiguous fragments) from the sequence seq. The sample function does not modify the original sequence.  $j = Random.sample (L, 3)#3 elements are randomly obtained from L and returned as a fragment - Print('J', J) # J [{' K1 ': ' v1 ', ' K2 ': ' V2 '}, ' a ', [2, 3]] - Print('L:', l) # L: [1, [2, 3], ' B ', ' a ', {' K1 ': ' v1 ', ' K2 ': ' V2 '}]

Python-Common modules-random module

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.