04-20-2019, 09:43 PM

While playing around with 41C MCODE, I've found a need for generating random numbers. Currently, I use the classic

to get a random number between 1 <= ran <= n. I perform these calcs using the OS system calls (MP2-10, INTFRC), and because I'm using binary numbers (not BCD) I also have to convert to/from binary to bcd using OS system calls (BCDBIN and GENNUM).

I've recently been pondering the inefficiency of all that and wondering if there is a better way to implement random numbers in MCODE using binary directly. Of course, the 41C CPU doesn't have multiplication or division as part of it's repertoire, and I know that it can be simulated with shifting, repeated addition and repeated subtraction, but I haven't gone down those rabbit holes very far.

I was considering doing some form of XORSHIFT algorithm for the random number generation, and then (and this is the part I'm really unsure of) some smart version of shifting and subtraction to do the modulo portion, and wondering if anyone has experience or ideas about random number generation in binary, particularly without multiply/divide?

I'm considering this for "simulation" usage, so I don't need a super long period, or cryptographically safe algorithm.

Code:

`ran = INT(seed * n) `

seed = FRC((seed * 9821) + .211327)

I've recently been pondering the inefficiency of all that and wondering if there is a better way to implement random numbers in MCODE using binary directly. Of course, the 41C CPU doesn't have multiplication or division as part of it's repertoire, and I know that it can be simulated with shifting, repeated addition and repeated subtraction, but I haven't gone down those rabbit holes very far.

I was considering doing some form of XORSHIFT algorithm for the random number generation, and then (and this is the part I'm really unsure of) some smart version of shifting and subtraction to do the modulo portion, and wondering if anyone has experience or ideas about random number generation in binary, particularly without multiply/divide?

I'm considering this for "simulation" usage, so I don't need a super long period, or cryptographically safe algorithm.