|
| 1 | +# Random |
| 2 | +import random |
| 3 | + |
| 4 | + |
| 5 | +# The random() method returns a random floating number between 0 and 1. |
| 6 | +# print(random.random()) |
| 7 | + |
| 8 | +# for integers randint() randrange() |
| 9 | +# print(random.randint(2,6)) |
| 10 | + |
| 11 | +# random.randrange(start, stop, step) |
| 12 | +print(random.randrange(2,8,2)) |
| 13 | + |
| 14 | +# random.choice(sequence) |
| 15 | +# random.sample(sequence, k) sequence Required. A sequence. Can be any sequence: list, set, range etc. && k Required. The size of the returned list |
| 16 | +# fruits = ['Apple', 'Banana', 'Orange', 'Grapes', 'Strawberry'] |
| 17 | +# print(random.choice(fruits)) |
| 18 | +# print(random.sample(fruits,2)) |
| 19 | + |
| 20 | +# random.seed(a, version) |
| 21 | +# *** Random.seed is not secure!!!!!!!!!!!!!!!!!! |
| 22 | +# a Optional. The seed value needed to generate a random number. |
| 23 | +# If it is an integer it is used directly, if not it has to be converted into an integer. |
| 24 | +# Default value is None, and if None, the generator uses the current system time. |
| 25 | + |
| 26 | +# version An integer specifying how to convert the a parameter into a integer. |
| 27 | +# Default value is 2 |
| 28 | + |
| 29 | +# random.seed(42) |
| 30 | +# print(random.random()) |
| 31 | + |
| 32 | +# shuffle |
| 33 | +# Shuffle a list (reorganize the order of the list items): |
| 34 | +# fruits = ['Apple', 'Banana', 'Orange', 'Grapes', 'Strawberry'] |
| 35 | +# print('Before Shuffle/ original fruits list', fruits) |
| 36 | +# random.shuffle(fruits) |
| 37 | +# print('Shuffled List', fruits) |
| 38 | + |
| 39 | +# uniform random.uniform(a, b) |
| 40 | +# a Required. A number specifying the lowest possible outcome |
| 41 | +# b Required. A number specifying the highest possible outcome |
| 42 | +# The uniform() method returns a random floating number between the two specified numbers (both included). |
| 43 | + |
| 44 | +# print('uniform', random.uniform(5,10)) |
| 45 | +# random.triangular(low, high, mode) |
| 46 | +# print(random.triangular(20, 60, 10)) |
| 47 | + |
| 48 | + |
| 49 | + |
0 commit comments