samedi 10 juillet 2021

Been using rand.int for a bit and seeing unexpected results

I've been running some code for an hour or so using a rand.int function, where the code models a dice's roll, where the dice has ten faces, and you have to roll it six times in a row, and each time it has to roll the same number, and it is tracking how many tries it takes for this to happen.

success = 0
times = 0
count = 0
total = 0

for h in range(0,100):
    for i in range(0,10):
        times = 0

        while success == 0:
            
            numbers = [0,0,0,0,0,0,0,0,0,0]
        
            for j in range(0,6):
            
                x = int(random.randint(0,9))
                numbers[x] = 1
        
                count = numbers.count(1)
            
            if count == 1:
                success = 1
            else:
                times += 1
    

        print(i)
        total += times
        success = 0


    randtst = open("RandomTesting.txt", "a" )
    randtst.write(str(total / 10)+"\n")
    randtst.close()




And running this code, this has been going into a file, the contents of which is below https://pastebin.com/7kRK1Z5f And taking the average of these numbers using

newtotal = 0
totalamounts = 0

with open ('RandomTesting.txt', 'rt') as rndtxt:  
    for myline in rndtxt:              ,
        newtotal += float(myline)
        totalamounts += 1

print(newtotal / totalamounts)

Which returns 742073.7449342106. This number is incorrect, (I think) as this is not near to 10^6. I tried getting rid of the contents and doing it again, but to no avail, the number is nowhere near 10^6. Can anyone see a problem with this?




Aucun commentaire:

Enregistrer un commentaire