MCODE: random numbers
04-20-2019, 09:43 PM
Post: #1
 RobertM Member Posts: 74 Joined: Jul 2016
MCODE: random numbers
While playing around with 41C MCODE, I've found a need for generating random numbers. Currently, I use the classic
Code:
ran = INT(seed * n)  seed = FRC((seed * 9821) + .211327)
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.
 « Next Oldest | Next Newest »

 Messages In This Thread MCODE: random numbers - RobertM - 04-20-2019 09:43 PM RE: MCODE: random numbers - John Keith - 04-20-2019, 10:12 PM RE: MCODE: random numbers - RobertM - 04-20-2019, 10:40 PM RE: MCODE: random numbers - Albert Chan - 04-21-2019, 01:35 AM RE: MCODE: random numbers - RobertM - 04-21-2019, 02:03 AM RE: MCODE: random numbers - Mark Power - 05-03-2019, 09:21 PM

User(s) browsing this thread: 1 Guest(s)