mathgenerator
mathgenerator copied to clipboard
Fix bad randomness
For some generators, the random value of the second value is limited by the random value of the first generated value. This can cause a bad distribution of generated problems, such as the second addend in addition()
being on average, less than the first addend.
I think that the best way to fix this is to get rid of things like maxSum
and generate terms independent of each other, but this may restrict use cases like elementary students who can only do single term addition not being able to generate problems with terms greater than 4 since you could generate two 5s and get a sum of 10.
By using the built-in function of the random values either .randint or .randrange you can achieve the question here is the code which can work
import random
def biased_addition(): first_addend = random.randint(1, 10) second_addend = random.randint(1, first_addend) return first_addend, second_addend
def balanced_addition(): first_addend = random.randint(1, 10) second_addend = random.randint(1, 10) return first_addend, second_addend
biased_result = [biasedaddition() for in range(1000)] balanced_result = [balancedaddition() for in range(1000)]
biased_average = sum(addend[1] for addend in biased_result) / len(biased_result) balanced_average = sum(addend[1] for addend in balanced_result) / len(balanced_result)
print("Biased Average:", biased_average) print("Balanced Average:", balanced_average)