Randgen¶
The Randgen module has a Generate class
Generate¶
Generate takes integer values as arguments (from no arguments up to and including five arguments) with optional string arguments being “r” or “s”. r for reverse(descending order) sorting and s for ascending order sorting. It has five methods which can be called on its instance namely:
Methods¶
- outcome() —-> generate outcomes based on the inputs supplied as arguments.
- unique() —-> generate unique outcomes based on the inputs supplied as arguments. You can think of it as a set of the outcome() method result.
- occur() —-> generate the number of times a unique item is found.
- getprob() —-> generate the probability of the outcome with respect to the unique outcome.
- getcum() —-> generate the cummulative probability of occurrence with respect to the unique outcome.
Importing the Generate class¶
You can import the Generate class in three ways:
import eventsim.randgen
from eventsim.randgen import Generate
from eventsim.randgen import *
and you can then respectively use it like this:
sample = eventsim.randgen.Generate()
sample = Generate()
The following examples will cover how arguments are used in the Generate class. In the examples, we assume that the class has been imported first as
from eventsim.randgen import Generate
Example 1a¶
zero arguments
What this does is populate the outcome list with random values (between 0 and 10) the default amount of times (i.e. a random number between 2 and 20).
sample = Generate()
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 1a¶
Outcome: [10, 4, 0, 5, 5, 2, 8, 4]
Unique Outcome: [10, 4, 0, 5, 2, 8]
Occurrence: [1, 2, 1, 2, 1, 1]
Probability: [0.25, 0.5, 0.25, 0.5, 0.25, 0.25]
Cummulative Probability: [0.25, 0.75, 1.0, 1.5, 1.75, 1.0]
Example 1b¶
Optional string argument
Usage: Generate (‘optional: sort ‘)
Adding an optional string argument “r” will sort the outcome in descending order whereas using the “s” string argument will sort the outcome in ascending order
sample = Generate("r")
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 1b¶
Outcome: [5, 3, 2, 2, 1]
Unique Outcome: [5, 3, 2, 1]
Occurrence: [1, 1, 2, 1]
Probability: [0.2, 0.2, 0.4, 0.2]
Cummulative Probability: [0.2, 0.4, 0.8, 1.0]
Example 2a¶
one argument
Usage: Generate (amount)
What this does is populate the outcome list with random values (between 0 and 10) the amount of times specified in the argument. For example if 10 was specified in the argument, it will populate the outcome list with values ten times
sample = Generate(8)
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 2a¶
Outcome: [8, 4, 8, 1, 2, 9, 2, 9]
Unique Outcome: [8, 4, 1, 2, 9]
Occurrence: [2, 1, 1, 2, 2]
Probability: [0.25, 0.125, 0.125, 0.25, 0.25]
Cummulative Probability: [0.25, 0.375, 0.5, 0.75, 1.0]
Example 2b¶
Optional string argument
Usage: Generate (amount, ‘optional: sort‘)
Adding an optional string argument “r” will sort the outcome in descending order whereas using the “s” string argument will sort the outcome in ascending order
sample = Generate(10,"s")
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 2b¶
Outcome: [0, 1, 1, 1, 3, 4, 7, 7, 9, 10]
Unique Outcome: [0, 1, 3, 4, 7, 9, 10]
Occurrence: [1, 3, 1, 1, 2, 1, 1]
Probability: [0.1, 0.3, 0.1, 0.1, 0.2, 0.1, 0.1]
Cummulative Probability: [0.1, 0.4, 0.5, 0.6, 0.8, 0.9, 1.0]
Example 3a¶
two arguments
Usage: Generate (start, stop)
What this does is populate the outcome list with values (between argument one and argument two) the default amount of times (i.e. a random number between 2 and 20).
sample = Generate(3, 7)
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 3a¶
Outcome: [4, 7, 7, 4, 3, 3, 4, 4]
Unique Outcome: [4, 7, 3]
Occurrence: [4, 2, 2]
Probability: [0.5, 0.25, 0.25]
Cummulative Probability: [0.5, 0.75, 1.0]
Example 3b¶
Optional string argument
Usage: Generate (start, stop, ‘optional: sort ‘)
Adding an optional string argument “r” will sort the outcome in descending order whereas using the “s” string argument will sort the outcome in ascending order
sample = Generate(2, 6, "r")
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 3b¶
Outcome: [6, 5, 5, 5, 4, 4, 4, 4, 2, 2]
Unique Outcome: [6, 5, 4, 2]
Occurrence: [1, 3, 4, 2]
Probability: [0.1, 0.3, 0.4, 0.2]
Cummulative Probability: [0.1, 0.4, 0.8, 1.0]
Example 4a¶
three arguments
Usage: Generate (start, stop, amount)
What this does is populate the outcome list with values (between argument one and argument two) the amount of times supplied in argument three. i.e. if 4 was supplied as the third argument, there will be four values in the outcome list.
sample = Generate(2, 5, 7)
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 4a¶
Outcome: [5, 5, 4, 4, 4, 2, 3]
Unique Outcome: [5, 4, 2, 3]
Occurrence: [2, 3, 1, 1]
Probability: [0.2857, 0.4286, 0.1429, 0.1429]
Cummulative Probability: [0.2857, 0.7143, 0.8572, 1.0]
Example 4b¶
Optional string argument
Usage: Generate (start, stop, amount, ‘optional: sort ‘)
Adding an optional string argument “r” will sort the outcome in descending order whereas using the “s” string argument will sort the outcome in ascending order
sample = Generate(2, 5, 7, "s")
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 4b¶
Outcome: [2, 3, 3, 4, 4, 5, 5]
Unique Outcome: [2, 3, 4, 5]
Occurrence: [1, 2, 2, 2]
Probability: [0.1429, 0.2857, 0.2857, 0.2857]
Cummulative Probability: [0.1429, 0.4286, 0.7143, 1.0]
Example 5a¶
four arguments
Usage: Generate (start, stop, step, amount)
What this does is populate the outcome list with values (between argument one and argument two) in steps of argument three value the amount of times in argument four’s value.
sample = Generate(2, 20, 3, 10)
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 5a¶
Outcome: [20, 8, 11, 5, 11, 17, 20, 17, 14, 20]
Unique Outcome: [20, 8, 11, 5, 17, 14]
Occurrence: [3, 1, 2, 1, 2, 1]
Probability: [0.3, 0.1, 0.2, 0.1, 0.2, 0.1]
Cummulative Probability: [0.3, 0.4, 0.6, 0.7, 0.9, 1.0]
Example 5b¶
Optional string argument
Usage: Generate (start, stop, step, amount, ‘optional: sort ‘)
Adding an optional string argument “r” will sort the outcome in descending order whereas using the “s” string argument will sort the outcome in ascending order
sample = Generate(2, 20, 3, 10, "r")
print ("Outcome:",sample.outcome())
print ("Unique Outcome:",sample.unique())
print ("Occurrence:",sample.occur())
print ("Probability:",sample.getprob())
print ("Cummulative Probability:",sample.getcum())
Result 5b¶
Outcome: [20, 20, 20, 17, 14, 11, 8, 5, 2, 2]
Unique Outcome: [20, 17, 14, 11, 8, 5, 2]
Occurrence: [3, 1, 1, 1, 1, 1, 2]
Probability: [0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2]
Cummulative Probability: [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0]