/ acting like // ?
Ducky47
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))
To copy+paste code into a post please:
copy+paste it
press that button in the edit menu labelled with
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 ( 2017-09-19 09:17:43 -0600 )edit
Thanks. I now know how to do that.
Ducky47 ( 2017-09-19 11:18:33 -0600 )edit
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)
In such cases please chose what is needed from the list:
sage: a = int(29); b = int(10)
sage: a/b
sage: QQ(a)/b
sage: RR(a)/b
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.
b = []
while count < 20:
while a < 6:
a = random.choice( [ 1..6 ] )
l.append( a )
# print l
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 #7: [5, 1, 6]
Trial #8: [5, 2, 4, 1, 2, 2, 2, 2, 1, 2, 3, 3, 3, 6]
Trial #10: [4, 2, 4, 5, 5, 6]
Trial #11: [5, 5, 3, 3, 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 #19: [3, 1, 3, 1, 2, 4, 5, 1, 5, 4, 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)
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 )
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 )
sage: sqrt(_).n()
This is a relatively high standard deviation.
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.
array numpy mixed division problem
Unexpected Answer for an Equation
divide numerator and denominator by certain value
How to get the quotients in multivariable polynomial division?
Division of polynomial matrices
dividing vector(a,b) by sqrt(x) gives (a/x*sqrt(x),b/x*sqrt(x))
Integer result for fraction
Using piecewise-defined functions for recursive integer sequence
[Solved] Difference about divide operator in python2 and sage