Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Note that using integers the division is the pythonic division, the result is also an integer. This may be the problem, if i figured it out as you need it. For instance:

sage: int(29)/int(10)
2

In such cases please chose what is needed from the list:

sage: a = int(29); b = int(10)
sage: a/b
2
sage: QQ(a)/b
29/10
sage: RR(a)/b
2.90000000000000

The N( ... ) should be applied on QQ(a)/b (sage) or

float(a)/b

if we want to give the pure python solution. The following code should work in the sense of the experiment, i've tried to not change too much starting from the initial one.

import random

count = 0;
b = []
while count < 20:
    a = 0
    l = []
    while a < 6:
        a = random.choice( [ 1..6 ] )
        l.append( a )
        # print l
    count += 1
    print "Trial #%s: %s" % ( count, l )
    b.append( QQ(len(l)) )

s = sum(b);
t = len(b)

print ( "b = %s\nsum(b) = %s throws = %s average = %s/%s = %s (approximatively %s)"
        % ( b, s, t, s, t, s/t, (s/t).n() ) )

(Yes, the browser also rejects my request to indent the code, had to do it in emacs with mark and Control+> in python mode... Sorry for the comment. Sometimes it does not work...)

Results this time:

Trial #1: [2, 3, 4, 2, 2, 4, 6]
Trial #2: [6]
Trial #3: [5, 6]
Trial #4: [5, 3, 4, 1, 4, 5, 1, 6]
Trial #5: [6]
Trial #6: [6]
Trial #7: [5, 1, 6]
Trial #8: [5, 2, 4, 1, 2, 2, 2, 2, 1, 2, 3, 3, 3, 6]
Trial #9: [5, 6]
Trial #10: [4, 2, 4, 5, 5, 6]
Trial #11: [5, 5, 3, 3, 6]
Trial #12: [3, 4, 2, 5, 6]
Trial #13: [3, 2, 5, 2, 6]
Trial #14: [4, 2, 5, 6]
Trial #15: [6]
Trial #16: [1, 1, 5, 1, 2, 5, 4, 4, 4, 1, 5, 5, 5, 2, 5, 5, 6]
Trial #17: [2, 5, 6]
Trial #18: [5, 5, 6]
Trial #19: [3, 1, 3, 1, 2, 4, 5, 1, 5, 4, 6]
Trial #20: [6]
b = [7, 1, 2, 8, 1, 1, 3, 14, 2, 6, 5, 5, 5, 4, 1, 17, 3, 3, 11, 1]
sum(b) = 100 throws = 20 average = 100/20 = 5 (approximatively 5.00000000000000)