Page 1 of 1

Random Number Generator does not seem to work?

Posted: Thu Dec 22, 2016 7:34 am
by SebSpiers
So the Random Number Generator doesn't seem to work, this is what I have done and it always returns the same number.

How do I generate a different 4 digit number every time???

Code: Select all

Set Variable rand to
Set Random Number Generator Constants = 1983, 2002
Set Random Number Generator Seed = 1337
rand = Generate Random Number (max value 9999)

Re: Random Number Generator does not seem to work?

Posted: Thu Dec 22, 2016 10:17 am
by FrancescoT
Dear SebSpiers,

please have a look at the following link where the same question was already discussed.
https://www.installaware.com/forums/viewtopic.php?f=2&t=10213&hilit=random+number

Regards

Re: Random Number Generator does not seem to work?

Posted: Thu Dec 22, 2016 10:47 am
by SebSpiers
This doesn't make sense to me, why have a "Random Number Generator" which does not have the ability to generate random numbers???

What's wrong with a good old rand() function?!!

I don't want to pay for a Cryptography plugin. :? :? :?

Re: Random Number Generator does not seem to work?

Posted: Thu Dec 22, 2016 11:29 am
by FrancescoT
Dear SebSpiers,

In fact it's "rand()" that gets used internally and with "rand()" you need to set a unique "seed" for each random generation. Otherwise rand() returns each time the same identical number.
http://stackoverflow.com/questions/9459035/c-rand-gives-same-number-when-running-process?rq=1

Hope this helps you.

Regards

Re: Random Number Generator does not seem to work?

Posted: Fri Dec 23, 2016 4:10 am
by SebSpiers
So how do I generate a random seed? to be used by the Random Number Generator? :)

Re: Random Number Generator does not seem to work?

Posted: Fri Dec 23, 2016 6:48 am
by FrancescoT
Dear SebSpiers,

please see the Serial Validation sample for detailed real-world usage of these functions.

Alternatively, you may create a custom dll that returns a value based on the current system time.

Hope this helps you.

Regards

Re: Random Number Generator does not seem to work?

Posted: Tue Jan 10, 2017 6:49 am
by SebSpiers
So I solved this by parsing the current time into 3 variables to give me a random number.

Code: Select all

Set Variable rand to
Set Variable PARSETEMP to
Set Variable const1 to
Set Variable const2 to
Set Variable const3 to
Set Variable cTIME to $TIME$
Parse String $cTIME$ into Variables const1 and PARSETEMP (Split at first occurrence of pattern)
Parse String $PARSETEMP$ into Variables const2 and const3 (Split at first occurrence of pattern)
Set Random Number Generator Constants = $const2$, $const3$
Set Random Number Generator Seed = $const1$$const1$
rand = Generate Random Number (max value 9999)

I know I could have done this in less lines, but this is how I'm doing it currently, the variables cTIME and PARSETEMP are probably not necessary. :mrgreen: