Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print values after each 1000 step

I want to print value after every certain interval (1000) on last line of code than every single value.

DARTS=200000
hits = 0
throws = 0
rangen = RanGenerator()
pi = 0
avg = 0
mu = 0
var = 0
dev = 1

for i in range (1, DARTS):
    throws += 1
    x = rangen.rand()
    y = rangen.rand()
    z = rangen.rand()
    tt = x**2 + y**2 + z**2
    dist = sqrt(tt)
    if dist <= 1.0:
        hits = hits + 1.0
        pi = 6 * (hits / throws)
        avg = avg + pi
        mu = avg/throws
        var = (var+(mu-pi)**2)/throws
        dev = sqrt(var)
        print("%d: %s" % (i,dev))
like image 676
Muhammad Sajid Iqbal Avatar asked Oct 22 '25 20:10

Muhammad Sajid Iqbal


1 Answers

This is easy with the modulo operator - it will print the values only when i is divisible by 1000:

if i % 1000 == 0:
    print("%d: %s" % (i,dev))
like image 187
grc Avatar answered Oct 25 '25 10:10

grc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!