Day 4: Randomisation and Lists
Last updated
Was this helpful?
Last updated
Was this helpful?
Python uses the Mersenne Twister, a pseudo-random number generator (PRNG) and is the most widely used PRNG.
If you go to and search for pseudo-random numbers there are loads of examples and information. What we need to do is import the random module (which incorporates the Mersenne Twister algorithm. No need to invent the wheel - might as well use a module :) We can generate random integers, floating point and shed load of others. Lets try some:
random.randint(1, 10)
Random integer between 1 and 10, INCLUDING BOTH random.randrange(1, 10)
Random integer in the range 1 and 10 INCLUDING 1 EXCLUDING 10
random.uniform(1, 10)
Random floating-point between 1 and 10, INCLUDING BOTH
random.random()
Random floating-point between 0.0 and 1.0 INCLUDING 0.0 EXCLUDING 1.0
What if we WANT random floating points ABOVE 1 and EXCLUDES THE UPPER NUMBER??
Well what if we multiply the floating random.random()
which is between (0 and 1) with an integer above 1 say like 5?? That would work
The python pseudorandom number generator uses the current time stamp as the seed. But we can in fact change this seed to what we want it to be
To check if your code is correct use these seeds for the following:
321 = Heads
435 = Tails
34567 = Tails
456 = Heads
These are very important as we will be using these many many times in Python. The python lists is called a "DATA STRUCTURE" which is a way of organising and storing data in Python We have already seen ways of storing SINGLE pieces of data using "variables"
Sometimes we want to store 'grouped' pieces of data, which has some sort of connection with each other. For example, you want to store all the States of the USA together as they are all related in the sense that they are all "united" even though different and thus making up the USA as we know it. Other cases, you might want to have "order' in your data, for example, you were storing all the people in a virtual queue, you don't want the last person jumping the virtual queue
So lists have "order" starting with the 1st item as being [0] with the second as [1] and so on We can also call on the last item in the list with a [-1], second to last [-2] etc
Lets say we want to change a spelling of an item:
monty_python_cast [2] = "Graeme Chapmann"
print(monty_python_cast)
We want to "add" and item to the end of a list (or delete)
monty_python_cast.append("Carol Cleveland") -----adds to the END of the list
monty_python_cast.remove("Terry Gilliam") -------removes the name etc
We want to extend a list with multiple items
monty_python_cast.extend(["Neil Innes", "Douglas Adams", "Connie Booth")] ---> adds to end
So what will happen is that the program will ask for a bunch of names separated by a comma and then it will randomly select a name from the program to pay.
Split String Method - REMEMBER - The divider (in this case commas ans a space between the names, can be changed to what you want)
For example there is a list called the "Dirty Dozen" whereby these fruits and vegetables still have pesticide residue on them even after they have been cleaned and washed etc and now available for consumption. This list is order are:
So from this list we can see it comprises of both Fruits and Veggies, and both of these lists belong to a bigger list of "high pesticide foods" called the "Dirty Dozen"
fruits = ["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Pears"]
vegetables = ["Spinach", "Kale", "Tomatoes", "Celery", "Potatoes"]
Here we can have lists, within lists - for example:
dirty_dozen = [ fruits, vegetables]
print(dirty_dozen)
Given this code: What will be printed out??
fruits [-1] = "Melons"
This statement DELETES "Pears" and REPLACES it with "Melons"
fruits.append("Lemons")
This statement ADDS on "Lemons" AFTER the new "Melons"
print(fruits)
["Strawberries", "Nectarines", "Apples", "Grapes", "Peaches", "Cherries", "Melons", "Lemons"]
AGAIN- have a look at the documentation