Masthead

Random Functions in Python (for gambling!)

 

1. Using Random Functions to Simulate Dice

Python comes with a package for generating random numbers. There are a variety of functions including uniform and normally distributed values.

To obtain a random number from a uniform distribution, we can use the "uniform(minimum,maximum)" function to generate values between a minimum and maximum value. Use the code below to generate a random number from a uniform distrubtion from 1 to 6 and then print out the result to the Debug I/O panel.

Import packages by putting the line "import package" where "package" is the name of the package you want to import, at the top of your script file. In this case you'll want to add "import random".

import random # include the library of random functions

Dice1=random.uniform(1,6) # create a random, floating-point number
print(Dice1) # print the random number

Notice that the values are floating point. We can convert them to integers (more on this later) with the "int()" function.

import random # include the library of random functions

Dice1=random.uniform(1,6) # create a random, floating-point number
Dice1=int(Dice1) # convert the number to an integer
print(Dice1) # print the random number

If you run the program a lot, you'll see that we rarely get the value 6. This is because our random.uniform() function is generating values FROM 1 TO 6. Which means we are getting fractional values like 1.0, 1.1, 1.2, 1.3, etc. These values will all be converted to 1. However, for 6, we are only getting 6.0 so there will be fewer values for 6 (i.e. we have loaded dice!). We can fix this by going between 1 and 6.99999.

import random # include the library of random functions

Dice1=random.uniform(1,6.9999999) # create a random, floating-point number
Dice1=int(Dice1) # convert the number to an integer
print(Dice1) # print the random number

Now, we just need to do the same calls for our second dice and then add them together:

import random # include the library of random functions
	
Dice1=random.uniform(1,6.9999999) # create a random, floating-point number
Dice1=int(Dice1) # convert the number to an integer
Dice2=random.uniform(1,6.9999999) # create a random, floating-point number
Dice2=int(Dice2) # convert the number to an integer
Total=Dice1+Dice2 # add the two integer numbers togeter
print(Total) # print the total of our two random numbers

Now, we can play dice games without the dice!

2. Analyzing Random Values

A bit part of current research is using computers to simulate things happening over and over again. We then analyize the results. Programming is great for this as computers don't mind doing the same thing thousands and even millions of times. In the code below, I have put our dice code into a loop and saved the results of rolling the dice 10,000 times. Then, I use a plotting library to display the distrubtion of the results. Try this and think about how you might use this to analyize data.

# import the librarys we will need

import random # include the library of random functions
import numpy # include the numpy stats library
import matplotlib.pyplot as pyplot

# Generate the values and store them in an array

TheArray=[] # create an array to hold the values
for Index in range(0,10000): # generate a lot of values
	Dice1=random.uniform(1,6.9999999) # create a random, floating-point number
	Dice1=int(Dice1) # convert the number to an integer
	Dice2=random.uniform(1,6.9999999) # create a random, floating-point number
	Dice2=int(Dice2) # convert the number to an integer
	Total=Dice1+Dice2 # add the two integer numbers togeter
	print(Total) # print the total of our two random numbers
	TheArray.append(Total) # add the total to the array
	
# create a histogram of the dice values
count, bins, ignored = pyplot.hist(TheArray, 11)

# create a plot and show it
pyplot.plot(bins, numpy.ones_like(bins), linewidth=2, color='r')
pyplot.show()

 

Additional Resources

Python Documentation

© Copyright 2018 HSU - All rights reserved.