dimanche 16 décembre 2018

Python randint()

Im trying to do some automatic test for my blockchain. I coded my random transaction function but it sometimes(!) crashes and sometimes not. I have no idea what is the problem...

def randomTransaction(users):

    rand1 = random.randint(0,4)
    rand2 = random.randint(5,9)
    sender = users[rand1]
    receiver = users[rand2]
    amount = random.randint(1,20)
    if sender.wallet < amount:
        while 1:
            amount = random.randint(1,20)
            if sender.wallet >= amount:
                break
    sender.wallet-= amount
    receiver.wallet+= amount
    return {"sender":sender.name, "receiver": receiver.name, "amount": amount}

Main program uses it alot:

for i in range(10):
    activeBlock = chain[i]
    z = 0
    while(len(activeBlock.transactionData)<=300):
        temp = randomTransaction(users)
        sender = temp.get("sender")
        receiver = temp["receiver"]
        amount = temp["amount"]
        activeBlock.__newTransaction__(sender, receiver, amount)
        z+=1

In "crash" case output looks like that, but no always...:

# 1 Crash:
Traceback (most recent call last):
  File "Test.py", line 43, in <module>
    temp = randomTransaction(users)
  File "Test.py", line 15, in randomTransaction
    amount = random.randint(1,20)
  File "/usr/lib/python3.6/random.py", line 217, in randint
    def randint(self, a, b):


# Another crash:
Traceback (most recent call last):
  File "Test.py", line 43, in <module>
    temp = randomTransaction(users)
  File "Test.py", line 15, in randomTransaction
    amount = random.randint(1,20)
  File "/usr/lib/python3.6/random.py", line 221, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.6/random.py", line 183, in randrange
    istart = _int(start)

It always refers to randint() implementation. What is the problem ?




Aucun commentaire:

Enregistrer un commentaire