我正在模拟指数衰减。为什么它不起作用?这是代码
from math import *
from random import random
from time import sleep
class atom():
def __init__(self, pos):
self.pos = pos
self.isalive = True
class substance():
def __init__(self, halflife):
self.halflife = float(halflife)
self.rate = float(log(2) / self.halflife)
self.life = float(0)
self.probdecay = float(0)
self.struct = [atom(i) for i in range(100)]
self.isalive = True
print self.rate
def sim(self):
print self.probdecay
for i in range(len(self.struct)):
if self.struct[i].isalive:
if random() < self.probdecay:
self.struct[i].isalive = False
self.life += 1.0
self.probdecay = 1.0 - pow(1.0 - self.rate, self.life)
def getstr(self):
s = ""
for i in self.struct:
if i.isalive:
s += "o"
else:
s += " "
return s
s = substance(5)
while s.isalive:
s.sim()
print s.getstr()
sleep(1)
这是输出
0.138629436112
0.0
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
0.138629436112
oooooo o ooooooooooo oooooooo ooooooo o oooooooooooo oooooo oooooooo oooo ooo oooooooo oooo oooo
0.258040751667
o ooo o oo oo ooo o ooo o ooooo o oo oooooo o oooo oo oooo oo ooo ooo ooo ooo oo
0.360898143882
o o oo oo o ooo ooo o oo o o oo o o o o o ooo o o oo o o oo
0.449496473814
o o o o o o o o o o o o oo
0.525812467226
o o o o o o
0.591548817506
o o
0.648172174615
0.696945867656
0.738958091134
0.775146183762
显然,这不是半衰期为 5 的指数衰减。这是怎么回事?