A more complex script generated by gpt4-turbo (#4)
Browse files- A more complex script generated by gpt4-turbo (469bfd6d406ec9bc74dca6eec66968f52db1c026)
Co-authored-by: 葉佐俊 <[email protected]>
- mathrandom2.py +70 -0
 
    	
        mathrandom2.py
    ADDED
    
    | 
         @@ -0,0 +1,70 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import random
         
     | 
| 2 | 
         
            +
            import json
         
     | 
| 3 | 
         
            +
            import operator
         
     | 
| 4 | 
         
            +
             
     | 
| 5 | 
         
            +
            # Define the number of samples you want to generate
         
     | 
| 6 | 
         
            +
            num_samples = 500000
         
     | 
| 7 | 
         
            +
            # Define the range for the random numbers
         
     | 
| 8 | 
         
            +
            min_value = -99.99
         
     | 
| 9 | 
         
            +
            max_value = 99.99
         
     | 
| 10 | 
         
            +
            # Define the arithmetic operations and their corresponding functions
         
     | 
| 11 | 
         
            +
            operations = {
         
     | 
| 12 | 
         
            +
                '+': operator.add,
         
     | 
| 13 | 
         
            +
                '-': operator.sub,
         
     | 
| 14 | 
         
            +
                '*': operator.mul,
         
     | 
| 15 | 
         
            +
                '/': operator.truediv
         
     | 
| 16 | 
         
            +
            }
         
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
            def generate_random_number():
         
     | 
| 19 | 
         
            +
                return float("%.3f" % random.uniform(min_value, max_value))
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
            def safe_division(numerator, denominator):
         
     | 
| 22 | 
         
            +
                return numerator if denominator == 0 else numerator / denominator
         
     | 
| 23 | 
         
            +
             
     | 
| 24 | 
         
            +
            # Generate complex data
         
     | 
| 25 | 
         
            +
            data = []
         
     | 
| 26 | 
         
            +
            for _ in range(num_samples):
         
     | 
| 27 | 
         
            +
                num1 = generate_random_number()
         
     | 
| 28 | 
         
            +
                num2 = generate_random_number()
         
     | 
| 29 | 
         
            +
                num3 = generate_random_number()
         
     | 
| 30 | 
         
            +
                # Ensure num2 and num3 are not zero if they will be used as divisors
         
     | 
| 31 | 
         
            +
                if num2 == 0.0:
         
     | 
| 32 | 
         
            +
                    num2 = generate_random_number()
         
     | 
| 33 | 
         
            +
                if num3 == 0.0:
         
     | 
| 34 | 
         
            +
                    num3 = generate_random_number()
         
     | 
| 35 | 
         
            +
             
     | 
| 36 | 
         
            +
                # Randomly choose two operations
         
     | 
| 37 | 
         
            +
                operation1 = random.choice(list(operations.keys()))
         
     | 
| 38 | 
         
            +
                operation2 = random.choice(list(operations.keys()))
         
     | 
| 39 | 
         
            +
                
         
     | 
| 40 | 
         
            +
                # Create a more complex expression
         
     | 
| 41 | 
         
            +
                if random.choice([True, False]):
         
     | 
| 42 | 
         
            +
                    # With parentheses
         
     | 
| 43 | 
         
            +
                    expression = f"({num1} {operation1} {num2}) {operation2} {num3}"
         
     | 
| 44 | 
         
            +
                    if operation1 == '/':
         
     | 
| 45 | 
         
            +
                        intermediate_result = safe_division(num1, num2)
         
     | 
| 46 | 
         
            +
                    else:
         
     | 
| 47 | 
         
            +
                        intermediate_result = operations[operation1](num1, num2)
         
     | 
| 48 | 
         
            +
                    if operation2 == '/' and intermediate_result != 0:
         
     | 
| 49 | 
         
            +
                        result = safe_division(intermediate_result, num3)
         
     | 
| 50 | 
         
            +
                    else:
         
     | 
| 51 | 
         
            +
                        result = operations[operation2](intermediate_result, num3)
         
     | 
| 52 | 
         
            +
                else:
         
     | 
| 53 | 
         
            +
                    # Without parentheses
         
     | 
| 54 | 
         
            +
                    expression = f"{num1} {operation1} {num2} {operation2} {num3}"
         
     | 
| 55 | 
         
            +
                    if operation1 == '/':
         
     | 
| 56 | 
         
            +
                        intermediate_result = safe_division(num1, num2)
         
     | 
| 57 | 
         
            +
                    else:
         
     | 
| 58 | 
         
            +
                        intermediate_result = operations[operation1](num1, num2)
         
     | 
| 59 | 
         
            +
                    if operation2 == '/':
         
     | 
| 60 | 
         
            +
                        result = safe_division(intermediate_result, num3)
         
     | 
| 61 | 
         
            +
                    else:
         
     | 
| 62 | 
         
            +
                        result = operations[operation2](intermediate_result, num3)
         
     | 
| 63 | 
         
            +
             
     | 
| 64 | 
         
            +
                output = "%.4f" % result
         
     | 
| 65 | 
         
            +
                data.append({'instruction': expression, 'output': output})
         
     | 
| 66 | 
         
            +
             
     | 
| 67 | 
         
            +
            # Create the dataset
         
     | 
| 68 | 
         
            +
            out_file = 'arithmetic-float-complex.json'
         
     | 
| 69 | 
         
            +
            with open(out_file, 'w') as f:
         
     | 
| 70 | 
         
            +
                json.dump(data, f)
         
     |