I am currently working on a python script to generate random dice rolls of two dice. It simulates 100 dice rolls and then takes the average or die1 die2 and the sum of the dice. It then outputs the results to a file in a "numbers" folder for 100 files. My problem is that each of the dies have a perfect 3 average and the sum is a perfect 6. How might I be able to make the results more random? (I want the averages to be a little off, which requires that the numbers be a little more random)
The Code
It generates a fairly clean text file with the results.Code:#!/usr/bin/python # Filename: randomdice.py #Copyright: Like2program 2008 import random aavg = 0 bavg = 0 cavg = 0 for y in range(1,101): filename = "numbers/numbers"+str(y)+".txt" open(filename, "w").write('# Dice 1 + Dice 2 = Sum:\n') aavg = 0 bavg = 0 cavg = 0 for x in range(1, 101): random.seed() a = random.randint(1,6) random.seed() b = random.randint(1,6) c = a+b aavg = aavg + a bavg = bavg + b cavg = cavg + c spam = str(x)+". "+str(a) + " + " + str(b) + " = " + str(c) + "\n" open(filename, "a").write(spam) aavg = aavg/100 bavg = bavg/100 cavg = cavg/100 spam = "Die 1 average: " + str(aavg) + " -- Die 2 average: " + str(bavg) + " -- Sum average:" + str(cavg) open(filename, "a").write(spam)
Another problem is that is sometimes makes a result with the sum listed as 7 when it is in fact 6. Help would be appreciated.
Thanks,
~like2
Edit:
Well, I have solved this mystery all on my own, using the wonderful tool that is floating point numbers!
By changing the averages initial values, I was able to make them floating points, and they now work just fine!
The working code is below
Code:#!/usr/bin/python # Filename: randomdice.py #Copyright: Like2program 2008 import random aavg = 0.0 bavg = 0.0 cavg = 0.0 for y in range(1,101): filename = "numbers/numbers"+str(y)+".txt" open(filename, "w").write('# Dice 1 + Dice 2 = Sum:\n') aavg = 0.0 bavg = 0.0 cavg = 0.0 for x in range(1, 101): random.seed() a = random.randint(1,6) random.seed() b = random.randint(1,6) c = a+b aavg = aavg + a bavg = bavg + b cavg = cavg + c spam = str(x)+". "+str(a) + " + " + str(b) + " = " + str(c) + "\n" open(filename, "a").write(spam) aavg = aavg/100 bavg = bavg/100 cavg = cavg/100 spam = "Die 1 average: " + str(aavg) + " -- Die 2 average: " + str(bavg) + " -- Sum average:" + str(cavg) open(filename, "a").write(spam)


LinkBack URL
About LinkBacks


Reply With Quote
