Random Module & Risk Analysis
LINKS
- How to use Random Module
- What is Risk Analysis
- Video: Big O Notation
- Python Random
- What is Dependency Injection
- W3Schools Random Modue
How to use Random Module
Random Functions
Randit
If we wanted a random integer, we can use the randint function Randint accepts two parameters: a lowest and a highest number. Generate integers between 1,5. The first value should be less than the second.
import random
print random.randint(0, 5)
Random
The random()
method returns a random floating number between 0 and 1.
If you want a larger number you can just multiply it:
import random
random.random() * 100
Choice
The choice()
method returns a randomly selected element from the specified sequence. The sequence can be a string, a range, a list, a tuple or any other kind of sequence.
import random
myList = [2, 109, False, 10, "Lorem", 482, "Ipsum"]
random.choice(myList)
Shuffle
The shuffle()
method takes a sequence (list, string, or tuple) and reorganize the order of the items.
import random
mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)
print(mylist)
Randrange
The randrange()
method returns a randomly selected element from the specified range.
random.randrange(start, stop, step)