mercredi 28 octobre 2020

generate random values for pre-generated sets of math instructions

So I'm working on generating random math statements for various int types. the equations use PEMDAS (** / * + -) and can have inner math statements nested in () several times. I also want the math equations to be able to use a predefined list of variables in it that i know the value of but don't have control over the value of. Since I'm doing this for each int type, i need to make sure that the generated math doesn't create an overflow/underflow for whatever type is being used.

I have functions (gen_add, gen_sub, gen_pow etc) that take the value being operated on and generate another value that can be safely used in the equation without causing an overflow/underflow. I also have functions (checked_add, checked_sub etc) that can be used to check if two numbers would cause an overflow/underflow.

That part alone would usually be easy but the issue is I want to make it so i can "reroll" each math equation so the syntax stays the same every time but the values within change. To do this I'm using seeded RNG to create the syntax of the math equation then using unseeded RNG to generate the values.

The problem I'm running into is that i cant control whats returned from () equations like i can raw values. This leads to a lot of bad equations for basically any generated instruction set. I cant simply bruteforce the equations with random values until i get a valid one because for larger int types (like u128/i128) that could take an immense amount of time. The general technique I'm trying to use right now is:

x = 43u8;                               the variables used in the equation (i know but don't control)
y = 55u8;                               
type = u8                               the type for the equation (always the same for each seed)

[]  + x    + y    - ([]  ** [] )        generate syntax (always the same for each seed)                                          
[]  + 43u8 + 55u8 - ([]  ** [] )        fill variables with their current values                                                       
      43u8 + 55u8 is valid              check if overflow has already occurred between adjacent filled values, return error if true      
????????????????????????????????        generate nested vals that wont cause overflow                                       
5u8 + 43u8 + 55u8 - (5u8 ** 2u8)        generate surface level vals that wont cause overflow     

so basically, for each generated instruction set i need to fill the placeholders so that the given variables (x, y in the example) never go out of their int types range (0-255 in the example) during the calculation. Ive been stuck on this for a while now. Does anyone have any ideas on how i could do this?

if it matters, I'm doing this in rust. but i feel that this is more of a logistics problem than a language problem.




Aucun commentaire:

Enregistrer un commentaire