Ask Your Question
0

/ acting like // ?

asked 2017-09-19 06:35:50 -0500

This script is meant to simulate rolling a fair die until a 6 occurs. I don't understand why at the very end my division of the sum of the rolls by the number of rolls is coming out the way it does. Thanks for your attention.

a=0;l=[];count=0;b=[] while count < 11: while a<6: a=choice([1..6]) l.append(a) print l count+=1 b.append(len(l)) a=0;l=[] s=sum(b);t=len(b) print "b= ",b,"Sum=",sum(b),"Throws=",len(b),"Sum/Throws=",s,"/",t,"=",s/t,N(s/t) print "Average over",len(b),"trials is",(sum(b)/len(b)) print "The type of sum(b) is",type(sum(b)),"The type of len(b) is",type(len(b))

edit retag flag offensive close merge delete

Comments

To copy+paste code into a post please:

  • copy+paste it
  • mark it
  • press that button in the edit menu labelled with

    101
    010
    

In python white spaces count, it is not easy to understand where is the problem "coming out the way it does", if one has first to arrange the spaces as they have to be.

dan_fulea gravatar imagedan_fulea ( 2017-09-19 09:17:43 -0500 )edit

Thanks. I now know how to do that.

Ducky47 gravatar imageDucky47 ( 2017-09-19 11:18:33 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-09-19 09:46:53 -0500

dan_fulea gravatar image

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)
edit flag offensive delete link more

Comments

The theoretical value can be computed after modeling the situation with a Markov chain with two states, the 6 and the rest R. The passage from R to R has probability $p=5/6$ and from R to 6 the complementary probability $q = 1-p = 1/6$. The probability to start in R and hit the 6 in exactly $n$ steps is $$ np^{n-1}q\ ,$$ so the theoretical expected number of steps needed is with sage:

sage: var( 'n' );
sage: p = 5/6; q = 1-p
sage: sum( n*p^(n-1)*q, n, 1, oo )
6

Yes, we know it, but statistically the variance of this experiment is not known, asked, considered... let us give it the line:

sage: sum( (n-6)^2*p^(n-1)*q, n, 1, oo )
30
sage: sqrt(_).n()
5.47722557505166

This is a relatively high standard deviation.

dan_fulea gravatar imagedan_fulea ( 2017-09-19 10:23:44 -0500 )edit

Thanks. I didn't appreciate the distinction between <type 'int'=""> and <type 'sage.rings.integer.integer'=""> . I see that len() always returns something of type 'int' and since the elements of my list "b" above were all created by len() they were of type 'int'. And the sum of elements of type 'int' is also of type 'int'. And that's how I wound up dividing one integer by another.

Thanks also for the infinite sum.

Ducky47 gravatar imageDucky47 ( 2017-09-19 12:18:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-09-19 06:35:50 -0500

Seen: 52 times

Last updated: Sep 19