47470.5112747213686085
4848# same values each time
4949
50+ # maximum 2 arguments: le premier étant la graine, le deuxième optionnel version
51+ seed (1 , 2 , 3 )
52+ for i in range (5 ):
53+ print (random ())
54+ # TypeError: Random.seed() takes from 1 to 3 positional arguments but 4 were given
5055
5156
52- ### random()
57+
58+ ### .random()
5359# produces a float number x, which falls within the range 0.0 ≤ x < 1.0
60+ # ne prend PAS d'argument
5461
5562import random
5663print (random .random ())
65720.9930295466333776
66730.023790533052207796
6774
75+ print (random .random (4 ))
76+ # TypeError: Random.random() takes no arguments (1 given)
77+
6878
6979
70- ### random. randrange(start, stop ,step)
80+ ### randrange(start, stop ,step)
7181# produces an integer number x, which is taken from the range start ≤ x < stop with step
7282# start argument defaults to 0
7383# step argument defaults to 1
8898
8999
90100
91- ### random .randint(start, stop)
101+ ### .randint(start, stop)
92102# stop inclus
93103
94104from random import randrange , randint
110120
111121
112122
113- ### random .choice(liste, tuple, string)
123+ ### .choice(liste, tuple, string)
114124
115- from random import choice
125+ from random import choice , choices
116126lst = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
117127tup = 20 ,23 ,24
118128var = "palestine"
129+
119130print (choice (lst )) # 1 element de la list
120131# 9
121132print (choice (tup )) # 1 elem du tuple
125136
126137
127138
128- ### random.sample(lst/tupple/string,int)
139+ ### .choices((liste, tuple, string), weights, k)
140+ # returns a list
141+ # weights == poids pour chaque elem de la list
142+ # k == nombre de réptition
143+ print (choices (lst , weights = [10 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ], k = 14 ))
144+ # [5, 1, 4, 9, 1, 10, 4, 5, 6, 10, 3, 1, 1, 9]
145+ print (choices (tup , weights = [10 ,1 ,1 ], k = 5 ))
146+ # [20, 23, 20, 20, 20]
147+
148+
149+
150+ ### .sample(lst/tupple/string,int)
129151from random import sample
130152print (sample (lst , 5 )) # 5 elements de la liste dans un sens au hasard
131153[10 , 9 , 8 , 1 , 4 ]
132154print (sample (lst , 10 )) # 10 elements de la liste dans un sens au hasard
133- [4 , 10 , 5 , 2 , 7 , 3 , 9 , 6 , 1 , 8 ]
155+ [4 , 10 , 5 , 2 , 7 , 3 , 9 , 6 , 1 , 8 ]
156+
157+ # avec k, positionnal argument
158+ print (random .sample ([ "spam" , "ham" , "eggs" ], k = 2 ))
159+ # ['eggs', 'spam']
160+ print (random .sample (k = 2 , [ "spam" , "ham" , "eggs" ] ))
161+ # SyntaxError: positional argument follows keyword argument
162+ print (random .sample ([ "spam" , "ham" , "eggs" ], 2 ))
163+ # ['ham', 'eggs']
0 commit comments