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