random (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

1.20 / 1.00.00


This function will generate a random number within a specified range.

 

The random number generated by this function must be in the range -2147483647 to +2147483647.

Invalid parameters will return 0.

 

 

Input:

lower : DINT

Lower bound for the random number to be generated.

 

upper : DINT

Upper bound for the random number to be generated.

 

Returns: DINT

Random number X, where: lower <= X <= upper.

 

 

Declaration:

FUNCTION random : DINT;
VAR_INPUT
lower : DINT;
  upper : DINT;
END_VAR;

 

 

Example:

//-----------------------------------------------------------------------------
// test.vpl, created 2008-03-03 17:56
//
// Generate and print out random numbers.
//
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
PROGRAM test;
 
  DebugFmt(message:="The dice shows: \4", v4:=random(lower:=1,upper:=6));
  DebugFmt(message:="Your lucky number is: \4", v4:=random(lower:=10,upper:=20));
 
END_PROGRAM;