Random numbers

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
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:
Sun Feb 18, 2018 6:35 pm
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.
I guess there will be some clue in the saved state file.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

ijabbott wrote:
Mon Feb 19, 2018 9:39 pm
Thomas Okken wrote:
Sun Feb 18, 2018 6:35 pm
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.
I guess there will be some clue in the saved state file.
The saved state will only tell you what the seed was when the state was saved, not where that value came from...
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:
Mon Feb 19, 2018 11:16 pm
ijabbott wrote:
Mon Feb 19, 2018 9:39 pm
Thomas Okken wrote:
Sun Feb 18, 2018 6:35 pm
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.
I guess there will be some clue in the saved state file.
The saved state will only tell you what the seed was when the state was saved, not where that value came from...
Yes, but if you've set the seed but haven't generated any random numbers from it, it should provide a clue about the internal value (15 digit number) generated from the parameter of SEED. As I understand it, that internal value isn't generated by anything resembling a secure hash function, and should be partly reversible.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Random numbers

Post by Thomas Okken »

ijabbott wrote:
Tue Feb 20, 2018 12:31 am
Yes, but if you've set the seed but haven't generated any random numbers from it, it should provide a clue about the internal value (15 digit number) generated from the parameter of SEED. As I understand it, that internal value isn't generated by anything resembling a secure hash function, and should be partly reversible.
That's true. The initial seed is derived from the return value of shell_random_seed() as follows:

seed = (abs(shell_random_seed()) % 1e14) * 10 + 1

The return value of shell_random_seed() is a signed 64-bit integer.
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:
Tue Feb 20, 2018 11:34 am
ijabbott wrote:
Tue Feb 20, 2018 12:31 am
Yes, but if you've set the seed but haven't generated any random numbers from it, it should provide a clue about the internal value (15 digit number) generated from the parameter of SEED. As I understand it, that internal value isn't generated by anything resembling a secure hash function, and should be partly reversible.
That's true. The initial seed is derived from the return value of shell_random_seed() as follows:

seed = (abs(shell_random_seed()) % 1e14) * 10 + 1

The return value of shell_random_seed() is a signed 64-bit integer.
Where can I find the seed, at which address? I've saved the machine state, opened it in a hex editor. But I have no clue where it's located.
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 »

Look for occurrences of random_number_low and random_number_high in core_globals.cc (definition) and core_math2.cc (where they are used, in the math_random() function), or also in core_commands2.cc (where they are set, in the docmd_seed() function).

To help find those variables without knowing their addresses, use INPUT or EDITN with long, distinctive variable names; those names are stored in memory shortly before the random seed. Also, you can confirm you've found the seed by comparing it to the last number returned by RAN.

It would be a lot simpler for SwissMicros to simply fix shell_random_seed(), but until they come back from vacation and release their source code, this kind of old-school detective work is the best you can do to satisfy your curiosity, I'm afraid.
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:
Tue Feb 20, 2018 1:52 pm
Look for occurrences of random_number_low and random_number_high in core_globals.cc (definition) and core_math2.cc (where they are used, in the math_random() function), or also in core_commands2.cc (where they are set, in the docmd_seed() function).

To help find those variables without knowing their addresses, use INPUT or EDITN with long, distinctive variable names; those names are stored in memory shortly before the random seed. Also, you can confirm you've found the seed by comparing it to the last number returned by RAN.

It would be a lot simpler for SwissMicros to simply fix shell_random_seed(), but until they come back from vacation and release their source code, this kind of old-school detective work is the best you can do to satisfy your curiosity, I'm afraid.
"vacation" ... "vacation" ... hmmm, maybe Webster tells me what this is ... ;)
Thanks for your help. I think I'll give it a try tonight.
Aaaaaah ... I love staying away some time for recreation, just to solve new problems when coming back to work *g*
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 »

Krauts In Space wrote:
Tue Feb 20, 2018 2:06 pm
"vacation" ... "vacation" ... hmmm, maybe Webster tells me what this is ... ;)
I thought you had decent amounts of paid vacation in Germany. Or does that not apply in Bavaria? :shock:
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:
Tue Feb 20, 2018 2:43 pm
Krauts In Space wrote:
Tue Feb 20, 2018 2:06 pm
"vacation" ... "vacation" ... hmmm, maybe Webster tells me what this is ... ;)
I thought you had decent amounts of paid vacation in Germany. Or does that not apply in Bavaria? :shock:
Sure - unless you're an self-employed ... :)
DM15L S/# 10584 FW v25
DM42 S/# 01015 FW v3.5
User avatar
ijabbott
Posts: 253
Joined: Fri Dec 15, 2017 2:34 pm
Location: GB-MAN

Re: Random numbers

Post by ijabbott »

Here are the offsets of random_number_low and random_number_high in the state file (at least for firmware version 3.3):
  • 0x16a random_number_low
  • 0x172 random_number_high
After 0 SEED, the saved state has:
  • random_number_low = 174081
  • random_number_high = 82418800
Working backwards from the code:

Code: Select all

        if (x == 0) {
            int8 s = shell_random_seed();
            if (s < 0)
                s = -s;
            s %= 100000000000000LL;
            s = s * 10 + 1;
            random_number_high = s / 10000000LL;
            random_number_low = s % 100000000LL;
            return ERR_NONE;
        }
There is one digit of overlap between random_number_high and random_number_low, i.e. the units digit of random_number_high is the same as the 10000000 digit of random_number_low (but only when seed is set via 0 SEED). So the line `s = s + 10 + 1;` produced the number 824188000174081 from 82418800017408.

So although we don't know the exact number returned by shell_random_seed(), we know its absolute value modulo 1e14 is 82418800017408.
Post Reply