Is there a way to generate a random number in KRL?
I've been using a work-around via RSI,
But no RSI in this project so was trying to figure out a way to hack one in
KR C4 compact
KSS 8.3.25
Is there a way to generate a random number in KRL?
I've been using a work-around via RSI,
But no RSI in this project so was trying to figure out a way to hack one in
KR C4 compact
KSS 8.3.25
Yes you can. You can use CWrite with $FCT_CALL as Parameter. It lets you call built-in functions like krl_getRandomSeed() and krl_getRandom(). Internally these call the c-style rand() function.
Please find in the attachment my implementation of KRL Interface functions that I usually use for this kind of purpose.
Fubini
Hm, not sure what I'm doing wrong, but the CWRITE function keeps spitting out a "Random Value Invalid" error.. Doesn't seem to matter if its inline, subprogram or function..
DECL E6POS rPos
...
rPos.X = RandomInt(0,300)
...
DEFFCT INT RandomInt(Min:in, Max:in)
DECL INT Min
DECL INT Max
DECL INT Random
DECL STATE_T State
DECL MODUS_T Mode
CWrite ($Fct_Call, State, Mode, "krl_getRandom", Random, Min, Max)
Return Random
ENDFCT
Display More
Am I doing something wrong? I'm not so versed in Interface functions(?) or anything that uses more than the single .src file so might be fudging something up..
Aaand nevermind, forgot to initialize Random. Didn't know that CWrite attempts to read the value before setting it (I think?)
Just put a Random=0 before the CWrite function and it works now thanks!
I just tried it and it works well
Thx