FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
02-28-2022 12:17 PM
Hi,
I would like to generate random numbers in a PLC, is there a function block available to generate random numbers?
Thx in advance
Solved! Go to Solution.
02-28-2022 01:45 PM
I found a library and an appropriate function block for my case:
CmpCrypto Implementation Library Documentation
Title: CmpCrypto Implementation
Version: 3.5.17.0
Author: CODESYS Development GmbH
with the function CmpCrypto_Implementation.CryptoGenerateRandomNumber(SIZEOF(u1), ADR(u1));
02-28-2022 01:55 PM
There is also a function RDM() in the OSCAT Basic library. See Codesys store for further information.
03-01-2022 09:45 AM
Hi,
as the implementation is a bit tricky, here is some example code.
PROGRAM PLC_PRG
VAR
uiNumOfRandomBytes : UDINT := 4;
Result : UDINT;
udiRandom2 : UDINT; // Random number
bsRandomNumber : RtsByteString := (ui32MaxLen:=SIZEOF(uiNumOfRandomBytes), ui32Len:=0, pByData:=ADR(udiRandom2)); //
END_VAR
// This code is working and generates a random number udiRandom2
Result := CryptoGenerateRandomNumber(ui32NumOfRandomBytes:= uiNumOfRandomBytes, pRandom:= ADR(bsRandomNumber));