simple-math / mathrandom-progressive.py
fblgit's picture
Create mathrandom-progressive.py
cb4d9ac verified
raw
history blame
2.11 kB
import random
import json
import operator
num_samples = [20000, 50000, 230000, 500000]
min_values = [-9.99, -19.99, -99.99, -99.99]
max_values = [9.99, 19.99, 99.99, 99.99]
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
operations = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv
}
def safe_division(numerator, denominator):
return numerator if denominator == 0 else numerator / denominator
for idx, _ in enumerate(num_samples):
data = []
for _ in range(num_samples[idx]):
num1, num2, num3 = None, None, None
while num1 in [None, 0., -0.] or num2 in [None, 0., -0.] or num3 in [None, 0., -0.]:
num1 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
num2 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
num3 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
operation1 = random.choice(list(operations.keys()))
operation2 = random.choice(list(operations.keys()))
if random.choice([True, False]):
expression = f"({num1} {operation1} {num2}) {operation2} {num3}"
intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
result = safe_division(intermediate_result, num3) if operation2 == '/' and intermediate_result != 0 else operations[operation2](intermediate_result, num3)
else:
expression = f"{num1} {operation1} {num2} {operation2} {num3}"
intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
result = safe_division(intermediate_result, num3) if operation2 == '/' else operations[operation2](intermediate_result, num3)
output = result_floats[idx] % result
data.append({'instruction': expression, 'output': output})
out_file = f'arithmetic-float-complex_{idx}.json'
with open(out_file, 'w') as f:
json.dump(data, f)