除夕安康(用Codesys随机数来发红包)
前言 首先,在这里给大家拜个早年,祝大家新一年,身体安康,财运亨通!!!今天用简短的时间来和大家分享一个Codesys下的产生随机数的函数。正文1. Code Show[*]1.1 Fc_Random
FUNCTION Fc_Random : UDINT
VAR_INPUT
END_VAR
VAR
bMarlonRando : UDINT;
mbs : CmpCrypto.CmpCrypto_Interfaces.RtsByteString := (ui32Len := 4, ui32MaxLen := 4, pByData := ADR(bMarlonRando));
Result : CmpCrypto.CmpCrypto_Implementation.CmpCrypto_Interfaces.SysTypes.RTS_IEC_RESULT;
END_VAR
//Requires CmpCrypto library
//Requires device vendor to have included Crypto component in Runtime
Result := CmpCrypto.CryptoGenerateRandomNumber(ui32NumOfRandomBytes:= 4, pRandom:= ADR(mbs));
Fc_Random := bMarlonRando;
[*]1.2 Pro_Main
PROGRAM Pro_Main
VAR
xStart : BOOL;
UdiMin : UDINT := 1;
UdiMax : UDINT := 50;
iToDo : INT ;
UdiOut : ARRAY OF UDINT ;
UdiTemp: UDINT;
UdiTotal : UDINT;
END_VAR
IF xStart THEN
UdiTotal := UdiMax ;
FOR iToDo := 1 TO 4 DO
UdiTemp := Fc_Random() ;//随机数函数
UdiOut := UdiTemp MOD UdiTotal ;
IF UdiOut = 0 THEN
UdiOut := UdiMin ;
END_IF ;
UdiTotal := UdiTotal - UdiOut ;
END_FOR ;
UdiOut := UdiTotal ;
xStart := FALSE ;
END_IF ;
[*]1.3 核心库 CmpCrypto Library
[*]1.4 Show
感觉微信红包的函数也类似啊~结尾 最后祝大家红包抢不停~(点个关注,不迷路~)
页:
[1]