// LK @ 07. Juli 2008
{Scl_OverwriteBlocks := 'y'} // Overwrite the blocks
{Scl_GenerateReferenceData := 'y'} // Generate reference data
{Scl_S7ServerActive := 'y'} // Take the "S7_server" server attribute into consideration
{Scl_CreateObjectCode := 'y'} // Create the object code
{Scl_OptimizeObjectCode := 'y'} // Optimize the object code
{Scl_MonitorArrayLimits := 'n'} // Monitor the array limits
{Scl_CreateDebugInfo := 'y'} // Create the debug info
{Scl_SetOKFlag := 'n'} // Set the OK flag
{Scl_SetMaximumStringLength := '254'} // Maximal string length
FUNCTION_BLOCK FB31
TITLE = 'RND_x'
// Generiert eine Zufalzzahl
VERSION : '1.0'
AUTHOR : lk
NAME : RND_x
FAMILY : S7SYSTEM
//KNOW_HOW_PROTECT
VAR_INPUT // Eingangsparameter
RND_MAX : INT;
RND_MIN : INT;
END_VAR
VAR_OUTPUT // Ausgangsparameter
RND_x : INT;
END_VAR
VAR // statische Variablen
q : DINT := 0;
END_VAR
VAR_TEMP // temporäre Variablen
x : REAL;
y : REAL;
z : DINT;
END_VAR
CONST // Konstanten
a := 1664525;
b := 2147483647;
c := 1013904223;
END_CONST
// Anweisungsteil
z := TIME_TO_DINT(TIME_TCK()); // TIME_TCK = SFC 64
;
IF q = 0 THEN
q := z;
END_IF
;
q := (a * (q + z) + c) MOD b;
x := DINT_TO_REAL(q) / DINT_TO_REAL(b);
y := x * INT_TO_REAL(RND_MAX - RND_MIN);
;
RND_x := REAL_TO_INT(ABS(y)) + RND_MIN;
END_FUNCTION_BLOCK