So I am making a function in python that simulates dice rolls based on user input. I have 2 versions of the code, one that uses a loop and one that is pure math(which I'm not sure is correct).
# Loop
def simulate_dice_loop(num, sides):
result = 0
while num > 0:
x = random.randint(1, sides)
result += x
num -= 1
return result
# Math
def simulate_dice_math(num, sides):
max = num * sides
result = random.randint(1, max)
return result
I'm highly suspicious if the math one is correct, especially on larger numbers. The loop dice roll
tends to stay around half of the max value. However, the math
one tends to fluctuate all the time, sometimes even reaching 90% of max-- which is probably highly unlikely on normal occassions.
...Please help on how I could improve the math
one because the loop tends to take longer the larger the number-- and also, to satisfy my curiosity on the subject. Thanks ^^
Aucun commentaire:
Enregistrer un commentaire