[CoLoCo] dice rolls in C

Jim Hutchinson jim at ubuntu-rocks.org
Sat Feb 9 19:00:36 GMT 2008


On Feb 8, 2008 11:13 PM, Kenneth D. Weinert <kenw at quarter-flash.com> wrote:
> change
>
> (rand(10) + 1) + (rand(10) + 1)
>
> to
>
> (rand(10) + rand(10))
>
> You *might* have to change the output as well, not sure.

I got it to run 2 10-sided dice but the output was a bit odd. It gave
results for 19 and 20 (0 each). Basically just not real clean but
probably not important.

Okay, I see how to do this in Ruby. Someone left a Python script on my
blog but I get errors I don't understand. Some about indents and I
know Python can be picky about that , and also "SyntaxError: Non-ASCII
character '\xe2' " which I don't get. Anyone know how to make this
work?

---begin---

from random import randrange

#Number of dice rolls
n = 10000

#13 because lists start at index 0, and we want hist[a] to contain the
number of rolls with result a, so hist[0] is always zero
hist = 13*[0]

for i in range(n):
#roll two dice
dice1 = randrange(1,7)
dice2 = randrange(1,7)
#calculate sum
sum = dice1 + dice2
#add one to the number of rolls with result sum
hist[sum] = hist[sum] + 1

for i in range(1,13):
print 'Dice Rolls with result %d' % i
print 'Total number: %d' % hist[i]
rel = float(hist[i])/n
print 'Relative Percentage: %f' % rel

---end---

Thanks,
-jim

-- 
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html



More information about the Ubuntu-us-co mailing list