ModSlaveSim help v3.04 - 4.4.7. Random numbers

Download manual: HTML

4.4.7. Random numbers

4.4.7.1. Random number generator

This example generates pseudo-random numbers between specified limits.

Table 20. Random number generator
Register Address Register Name Statement
$100 Minimum value  
$101 Maximum value  
$102 Value ($103 / 4294967296) * ($101 - $100) + $100
$103 Sequence $$ * 1103515245 + 12345
  • Sequence is a pseudo-random sequence of values in the range 0..4294967295, generated using the "linear congruential" method. Sequence must be a uint32 register for this to work.

  • The Value statement scales/offsets Sequence to be in required range.

4.4.7.2. Random walk

This example generates a random walk by randomly generating increments to a value. This template would be of use in ModSlaveSim for something like ambient temperature fluctuations, which vary around the preceding temperature (rather than fluctuating wildly from -40 degrees C to 30 degrees C in a single step).

Table 21. Random number generator
Register Address Register Name Statement
$100 Maximum change  
$101 Value
$$ + ( (($102 / 4294967296) * 
$100 * 2 - $100) * CycleTime)
$102 Sequence $$ * 1103515245 + 12345
  • The Maximum change parameter specifies the maximum change in the random number that is generated per second. Sequence generates the random numbers.

  • The Value statement scales the Sequence value to be in the required range. Multiplying by CycleTime makes the behaviour of the generator independent of the configured cycle time. A breakdown of the value statement is shown below:

    Code Segment:	    Function:
    $102/4294967296 : No. in range 0 to 1 (Sequence/max uint32) 
    * $100 		: No. in range 0 to 0.02 (Maximum ch. == 0.02)
    * 2 - $100	: No. in range -0.02 to 0.02
    * CycleTime	: The change per second
    $$ +		: Adds the change obtained to itself (Value)

To apply this to an example requiring random temperature fluctuations, a Maximum change value would be the maximum temperature change within a second that could occur in the system being modelled. You would also need to consider if the temperatures can go up as well as down. If the temperatures can only go up (so random numbers should always be up) then the statement in Value would need to be modified to reflect this.