Sign in to follow this  
Guest Ron Freimuth

Rand in XML

Recommended Posts

Nick had asked that I post my Random Macro. I converted a Fortran Rand function to this: 785398165 (L:Ran,number) * 422649731 + 2000000000 % d (>L:Ran,number) (* Saved Variable *) 2000000000 / (*out*) The top of the stack contains the result. In the range of 0.000 to 1.000. Shorter intergers would do OK, but I only had the double precision function available. The integers are special values thata give a good random distribution. XML supports the length OK and shorter variables would probably not be any faster. I tested it and the average value of many calls approaches 0.50. The 'RMS' is also correct. Approaching 1/sqrt(3). One could use "@Rand 0.1 * 0.05 -" to add a randomness to, say a gauge variable. Adding the above to the value would result in a new variable that would move around +/- 0.05 from the normal value. Each invocation generates a new Random on the stack. Note there is no way to seed the above Macro. However, that could be added if one needed the sequence to start at an arbitrary value. Ron

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Thanks Ron. This demonstrates the principal, 785398165 (L:Ran,number) * 422649731 + 2000000000 % d (>L:Ran,number) (* Saved Variable *) 2000000000 / (*out*) %(@Rand 0.1 * 0.05 -)%!+4.2f!

Share this post


Link to post
Share on other sites

>Thanks Ron. This demonstrates the principal, Glad it worked. ;)>>>> >> 785398165 (L:Ran,number) * 422649731 + 2000000000 % d> (>L:Ran,number) (* Saved Variable *)> 2000000000 / (*out*) > (Display result in message above) Now most 'random' variations follow the 'Normal', 'Bell Curve'. One can approximate this distribution by adding several rectangular distributions (such as from @Rand), then dividing by sqrt (n). @Rand @Rand @Rand + + 3 sqrt / Untested, but should give a distribution that varies from -3/sqrt(3) to +3/sqrt(3). One can replace the "3 sqrt /" with "0.57735 *", so the stack top varies from -1.7321 to 1.7321. But, peaked at 0.000. Both the Variance and RMS should be 1.00. If one added a little variation to either the throttle inputs or the RPM readings on multi-engine AC, he would then eliminate the RPM, fuel flow, etc. from being exactly the same for each powerplant. A slight offset might also be reasonable. Or, only a random offset might be added to each of the throttle settings. Generally, the RPM's, etc. are pretty steady in real AC, but are never exactly the same. After all, the throttles are not locked as in FS. I also created AVG and RMS Macros. Actually, I wanted to see the 'RMS' variation of Commanded pitch against Actual pitch when turbulence was present. I later used them on my Rand to check it out. One needs a variation in the name and internal L:vars for each Average done. (* Updates AVG every 4 seconds *) (L:AVG1_Init,bool) ! if{ 1 (>L:AVG1_Init,bool) 1 (>L:NA1,enum) 0 (>L:AV1,number) } @1 (L:AV1,number) + d (>L:AV1,number) (L:NA1,enum) / (* Add to Average *) (L:NA1,enum) @2 >= (* Every Samplers/18.2 *) if{ (>L:AVG1,number) 0 (>L:AV1,number) 0 (>L:NA1,enum) } (* Average *) (L:NA1,enum) ++ (>L:NA1,enum) (* @AVG1(Var,Samples) *) 'Samples' refers to number of times the Macro averages the '@1' input. At 18.2 ticks per second, the below would put a new Average in (L:Random,number) every four seconds:@AVG1(@Rand,72) (L:AVG1,number) (>L:Random,number) Note AVG1's output is in (L:AVG1,number), not at the top of the stack. A small variation could leave it on the stack, so the middle term above wouldn't be needed. A 'Running Average' (exponentially weighted filter) is even shorter; it weights recent values more than values farther in the past, and has the advantage that it gives a new value on every tick. Essentially, it amounts to a Low Pass Filter, filtering out noise depending on the Time Constant (TC) set. However, the "Filter" also requires two internal weighting factors to be calculated, and that adds some to the complexity. So, I won't lengthen this message even more with the code. I expect FS uses a similar Function to make things like CHT come up slowly, rather than all at once. I ID'ed one CHT parameter in the AIR file as '1/TC'. Similar for Oil Temp, EGT, etc. Ron

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this