Random numbers

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Krauts In Space
Posts: 85
Joined: Wed Jan 03, 2018 3:48 pm
Location: Nuremberg, Germany

Random numbers

Post by Krauts In Space »

Well, everybody knows how to create same "random numbers" over and over agin: "SEED $samenumberofyourchoice"

Does anybody know a better way than

Code: Select all

01 LBL "RNDM"
02 DATE
03 TIME
04 *
05 SEED
06 RAN
07 RTN
Any more randoms to create a better seed?

Thinking about it, maybe

Code: Select all

01 LBL "RNDM"
02 DATE
03 TIME
04 *
05 TAN
06 ABS
07 1
08 MOD
09 RTN
would do fine.
DM15L S/# 10584 FW v25
DM42 S/# 01015 FW v3.5
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

Just use 0 SEED. That seeds the random number generator using the real-time clock.

How exactly it does this depends on how the shell_random_seed() function is implemented, and that is part of the platform-specific code, so we don't know (yet) how this works on the DM42.

On the platforms that Free42 supports, it uses the current time, in milliseconds since whatever epoch the platform uses, modulo 1e14, generating unique seeds on a 3168-year cycle.
User avatar
ijabbott
Posts: 253
Joined: Fri Dec 15, 2017 2:34 pm
Location: GB-MAN

Re: Random numbers

Post by ijabbott »

Thomas Okken wrote:
Wed Feb 14, 2018 12:39 pm
Just use 0 SEED. That seeds the random number generator using the real-time clock.

How exactly it does this depends on how the shell_random_seed() function is implemented, and that is part of the platform-specific code, so we don't know (yet) how this works on the DM42.
It doesn't seem to work on DM42 with current (V3.3) firmware at least. 0 SEED RAN RAN RAN produces the same sequence each time.
User avatar
ijabbott
Posts: 253
Joined: Fri Dec 15, 2017 2:34 pm
Location: GB-MAN

Re: Random numbers

Post by ijabbott »

Some STM32 microcontrollers have a built-in hardware RNG. If the DM42 has one, perhaps the shell_random_seed() function should use it.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

ijabbott wrote:
Wed Feb 14, 2018 6:09 pm
Thomas Okken wrote:
Wed Feb 14, 2018 12:39 pm
Just use 0 SEED. That seeds the random number generator using the real-time clock.

How exactly it does this depends on how the shell_random_seed() function is implemented, and that is part of the platform-specific code, so we don't know (yet) how this works on the DM42.
It doesn't seem to work on DM42 with current (V3.3) firmware at least. 0 SEED RAN RAN RAN produces the same sequence each time.
It's possible that they didn't notice the API change in 2.0.7, when shell_random_seed() changed from returning a double in the range [0, 1) to returning a 64-bit int. Assuming that doesn't cause a compilation failure outright, I could see it seeding the sequence with 0 every time.

It might be interesting to compare a firmware based on Free42 < 2.0.7 (that's DM42 < 3.1), but basically, if 0 SEED produces the same sequence each time, that's a bug in DM42.
Last edited by Thomas Okken on Sun Feb 18, 2018 4:26 am, edited 1 time in total.
Krauts In Space
Posts: 85
Joined: Wed Jan 03, 2018 3:48 pm
Location: Nuremberg, Germany

Re: Random numbers

Post by Krauts In Space »

For the Moment a TIME SEED RAN might help.

Any chance to generate a true random# just with RAN and make SEED obsolete (yet still usable for compatibility reason)?
Or are reproducable "random" numbers essential, e.g. for testing code?

Bug?
I wrote my little "randomseeder"
TIME SEED RAN - and the output makes me wonder.
With FIX 11 the 3 rightmost digits always are 950.
That let me do a little investigation.
SEEDing 100, 101, 500, 800 leaves 838505878 at the rightmost.
0.2 and 0.3 leave 0.40385113093 and 0.10385113093. Only the first digit after the decimal point differs.
0.8? Same, 0.60385113093
I don't have time nor I'm a mathematican with a "Random" Degree :D but this looks strange to me.
Is this an issue since v3.3 or earlier?
DM15L S/# 10584 FW v25
DM42 S/# 01015 FW v3.5
User avatar
Walter
Posts: 3070
Joined: Tue May 02, 2017 11:13 am
Location: On a mission close to DRS, Germany

Re: Random numbers

Post by Walter »

Krauts In Space wrote:
Thu Feb 15, 2018 9:59 am
I don't have time nor I'm a mathematican with a "Random" Degree :D but this looks strange to me.
:lol:
You're right, the outputs look fishy. :?
WP43 SN00000, 34S, and 31S for obvious reasons; HP-35, 45, ..., 35S, 15CE, DM16L S/N# 00093, DM42β SN:00041
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

Krauts In Space wrote:
Thu Feb 15, 2018 9:59 am
Bug?
It uses the same algorithm as the real HP-42S, which is the same as the RPL calculators, except for using a different starting seed after Memory Clear.

I implemented this in 2.0.7 (DM42 3.1); earlier versions used the RNG from the HP-41C applications book: r[n+1] = frac(r[n]*9821+0.211327).

The RPL/42S algorithm, and the way RDZ/SEED work, are explained in agonizing detail here: https://groups.google.com/forum/m/#!msg ... tzMtZhlGoJ.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

I tried changing shell_random_seed() so it always returns zero, but that causes a different sequence than the one produced by the DM42 after 0 SEED.

It still means that the DM42 shell_random_seed() is broken, just not in the way it would be if the 2.0.7 API change were the problem. Maybe it uses the xkcd random algorithm. ;)
Krauts In Space
Posts: 85
Joined: Wed Jan 03, 2018 3:48 pm
Location: Nuremberg, Germany

Re: Random numbers

Post by Krauts In Space »

Thomas Okken wrote:
Sat Feb 17, 2018 5:56 pm
Krauts In Space wrote:
Thu Feb 15, 2018 9:59 am
Bug?
It uses the same algorithm as the real HP-42S, which is the same as the RPL calculators, except for using a different starting seed after Memory Clear.

I implemented this in 2.0.7 (DM42 3.1); earlier versions used the RNG from the HP-41C applications book: r[n+1] = frac(r[n]*9821+0.211327).

The RPL/42S algorithm, and the way RDZ/SEED work, are explained in agonizing detail here: https://groups.google.com/forum/m/#!msg ... tzMtZhlGoJ.
Has the SEEDing on the DM42 prior to 2.0.7 ever been tested?
DM15L S/# 10584 FW v25
DM42 S/# 01015 FW v3.5
Post Reply