ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 19 Sep 2017 12:18:44 -0500/ acting like // ?https://ask.sagemath.org/question/38853/acting-like/ 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))Tue, 19 Sep 2017 06:35:50 -0500https://ask.sagemath.org/question/38853/acting-like/Comment by Ducky47 for <p>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.</p>
<p>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))</p>
https://ask.sagemath.org/question/38853/acting-like/?comment=38860#post-id-38860Thanks. I now know how to do that.Tue, 19 Sep 2017 11:18:33 -0500https://ask.sagemath.org/question/38853/acting-like/?comment=38860#post-id-38860Comment by dan_fulea for <p>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.</p>
<p>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))</p>
https://ask.sagemath.org/question/38853/acting-like/?comment=38856#post-id-38856To 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.Tue, 19 Sep 2017 09:17:43 -0500https://ask.sagemath.org/question/38853/acting-like/?comment=38856#post-id-38856Answer by dan_fulea for <p>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.</p>
<p>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))</p>
https://ask.sagemath.org/question/38853/acting-like/?answer=38858#post-id-38858Note 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)
Tue, 19 Sep 2017 09:46:53 -0500https://ask.sagemath.org/question/38853/acting-like/?answer=38858#post-id-38858Comment by Ducky47 for <p>Note that using <strong>integers</strong> 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:</p>
<pre><code>sage: int(29)/int(10)
2
</code></pre>
<p>In such cases please chose what is needed from the list:</p>
<pre><code>sage: a = int(29); b = int(10)
sage: a/b
2
sage: QQ(a)/b
29/10
sage: RR(a)/b
2.90000000000000
</code></pre>
<p>The <code>N( ... )</code> should be applied on <code>QQ(a)/b</code> (sage) or </p>
<pre><code>float(a)/b
</code></pre>
<p>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.</p>
<pre><code>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() ) )
</code></pre>
<p>(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...)</p>
<p>Results this time:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/38853/acting-like/?comment=38862#post-id-38862Thanks. 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.Tue, 19 Sep 2017 12:18:44 -0500https://ask.sagemath.org/question/38853/acting-like/?comment=38862#post-id-38862Comment by dan_fulea for <p>Note that using <strong>integers</strong> 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:</p>
<pre><code>sage: int(29)/int(10)
2
</code></pre>
<p>In such cases please chose what is needed from the list:</p>
<pre><code>sage: a = int(29); b = int(10)
sage: a/b
2
sage: QQ(a)/b
29/10
sage: RR(a)/b
2.90000000000000
</code></pre>
<p>The <code>N( ... )</code> should be applied on <code>QQ(a)/b</code> (sage) or </p>
<pre><code>float(a)/b
</code></pre>
<p>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.</p>
<pre><code>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() ) )
</code></pre>
<p>(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...)</p>
<p>Results this time:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/38853/acting-like/?comment=38859#post-id-38859The 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.Tue, 19 Sep 2017 10:23:44 -0500https://ask.sagemath.org/question/38853/acting-like/?comment=38859#post-id-38859