rockym93 dot net

archive · tags · feed

A dicey situation.

18 August 201009:19AMcode

Oh look, I made a pun.

So here's the thing. Maths teach sets this exercise where we use the random number generators on the calculator to generate a bunch of random numbers, add them together 4 at a time, do this 100 times and tally the result in order to demonstrate random data distributions. I figured there's an easier, and since we're already using the calculator pseudo-RNG and not a real random number, I thought what the hell and wrote this:

#!/usr/bin/env python  
#A thing to roll some dice.

import random  
import sys  
def diceRoller(s,n):  
 r = 0  
 for i in range(n):  
  r = int(r) + random.randint(1,6)  
 return r

if len(sys.argv) != 4:  
 print "Usage: xdy.py [number of dice] [number of sides] [number of rolls]"  
 exit()

dice = int(sys.argv[1])  
sides = int(sys.argv[2])  
rolls = int(sys.argv[3])  
maxroll = dice*sides  
rawdata = list()

for i in range(rolls):  
 rawdata.append(diceRoller(sides,dice))  
#print rawdata  
rawdata.sort()  
graphdata = dict()  
for i in range(maxroll):  
 graphdata[i+1] = rawdata.count(i+1)  
#print graphdata  
for i in graphdata:  
 print str(i) + ":",  
 if rolls < 1000:  
  for j in range(graphdata[i]):  
   print "#",  
  print  
 else:  
  print graphdata[i]

So, a couple of points here. Key point one: I am a bit of a fail-programmer, so if you look at this and think "WTF" that's probably why.

Key point two: After having posted this I realise that sorting the data before counting it wasn't really necessary (I think that's a leftfover from the first attempt where I just printed the data straight out.). Which brings me to...

Key point three: I started this with 100 million rolls on my netbook about half an hour ago, and it maxxed the CPU, and it's still going. Pretty sure that's cause of the sorting-which-doesn't-need-doing. Gosh darn it.

Key point four: I chose 100 million because it runs out of memory at... erm, a thousand million? A billion? What do we call it? 10^9, anyway.

I plan to print out my final list of numbers and show it to the teacher, just for shiggles.

Speaking of list: it just finished. So ladies and gentlemen, I proudly present.... drum roll 100000000 rolls of 4d6!

Oh. It didn't finish. It crashed. Never mind, here's a million to tide you over:

1: 0
2: 0
3: 0
4: 780
5: 3204
6: 7711
7: 15402
8: 27017
9: 43364
10: 62143
11: 79685
12: 96621
13: 107970
14: 112631
15: 108367
16: 96390
17: 80221
18: 61420
19: 43159
20: 26855
21: 15331
22: 7872
23: 3079
24: 778

Now I should probably go and do something useful.

< On Paths Roman Newsflash. >