Python's random function

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 3 #random use of various methods4 ImportRandom5  6 #random generation of floating-point numbers [0.1]7 Print("random ():", Random.random ())8  9 #randomly generates an integer between 1000-9999Ten Print("Randint (9999):", Random.randint (1000, 9999)) One   A #randomly generate an even number between 0-20 - Print("randrange (0, 2):", Random.randrange (0, 21, 2)) -   the #randomly generate floating-point numbers between 0-20 - Print("Uniform (0,):", Random.uniform (0, 20)) -   - #randomly select an element from a sequence +List_string = ['a','b','C','D','e'] - Print("Choice (list):", Random.choice (list_string)) + Print("Choice (string):", Random.choice ('ABCD')) A   at #random Ordering of list elements -List_number = [1, 2, 3, 4, 5] - random.shuffle (List_number) - Print("Shuffle (list):", List_number) -   - #randomly fetches a fragment of a specified length from a specified sequence in Print("sample (Sequence):", Random.sample ('ABCDEFG', 2))

Results:

Random (): 0.39840036736565154randint (+, 9999): 5736, 2): 220): 15.785447740011865 Choice (list): Bchoice (String): Bshuffle (list): [5, 3, 4, 1, 2]sample (sequence): ['F  ['b']

Write a Build Verification code:

  1  import   random   2  def   v_ Code ():   3  code= "  4  for  i in  range (5< Span style= "color: #000000"):   5  code+=str (Random.choice ([Random.randra Nge (Ten), Chr (Random.randrange (65,91  6   7  print    8   9  10  v_code () 

Results:

04tt3

Python's random function

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.