[CoLoCo] dice rolls in C

Jim Hutchinson jim at ubuntu-rocks.org
Wed Feb 13 04:49:05 GMT 2008


Okay, one more thing. The following python script prints out the
totals for the values 1-6 for 1000 "rolls". How can I bias it? I was
thinking an if-then statement that checked to see if 6 was chosen and
50% of the time it re-rolls and 50% accepts it. There may be a better
way as well - for example assigning "weights" to each value such as
15% for 1, 15% for 2 ... and 25% for 6. Any suggestions?

---begin---

import random

def roll(how_often):

    rolls = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0}

    for i in xrange(how_often):
        key = random.choice(range(6)) + 1
        rolls[key] += 1

    print rolls

roll(1000)

---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