lukew3 / mathgenerator

A math problem generator, created for the purpose of giving self-studying students and teaching organizations the means to easily get access to high-quality, generated math problems to suit their needs.
https://lukew3.github.io/mathgenerator
MIT License
690 stars 178 forks source link

Fix bad randomness #388

Open lukew3 opened 1 year ago

lukew3 commented 1 year ago

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.

YousafZahid1 commented 1 year ago

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)