Post Reply 
(DM42) Lottery number generator programming
02-11-2019, 07:35 PM (This post was last modified: 02-11-2019 07:45 PM by Dieter.)
Post: #2
RE: (DM42) Lottery number generator programming
(02-10-2019 09:54 AM)psw Wrote:  I am a sheer beginner of DM42 programming and interesting in a simple and useful program.
How you combine or improve these, I only know a little about 8-bit BASIC.
Thank you for reading and I look forward to hearing your great help.

I see this is your first post here, so welcome to the forum.

Regarding the intended program, I think we have to start from scratch here. There seem to be several misconceptions about various DM-42 (or Free42, or HP-42s) commands. For instance it is not a good idea at all to seed the random number generator before every single RAN call. Essentially you can forget about SEED unless you want to reproduce a certain random number sequence. Also your code would not generate integers between 1 and 18 but between 0 and 17 – you have to add 1 here. Then there are loop counters DSE and ISG so that manually decrementing R00 and checking whether it is > 0 or not is not required. This works like a FOR-loop in other programming languages. Finally ASTO does not store a value into Alpha, but it stores the first six characters of Alpha into the specified register (that's why the command then reads ASTO 03 or ASTO ST X or whatever). So the code snippets you posted will not work, neither separately nor combined.

OK, let's forget about all this. The program you want would have to behave like this:

Generate X random integers between 1 and Y

Do the following loop X times
   Generate a random integer R between 1 and Y
   If this is the first loop, skip the following test
      For all previous random integers
         Check if R equals one of these
         If it does, jump back to the main loop and generate a new random integer
         Proceed to next previously calculated random integer
      End of test loop
   At this point no match has occured, so save R in the result list
   Append R to the output string
End of main loop
Display output string

The following Free42 program implements this algorithm. The max. random integer Y is stored in "MAX", flag 01 is used to mark the first loop, it is automatically cleared when tested, and the loops are controlled by ISG and DSE. The loop counter is in R00, the test loop counter is on the stack. The random integers are stored in R01, R02, R03 etc. If, say, the 4th number has been calculated, the program compares this to R03, R02 and R01 to see whether there are duplicates. If there are, a new number is calculated. If not, the new R is stored in the respective register, here R04. The first two lines correct input errors ("18 different numbers out of 4" instead of vice versa) and the final lines restore the original Y and X and remove "MAX" (cleanup). This way you can repeat the whole thing by simply pressing [R/S].

This program can be optimized. For instance the whole flag thing can be removed. ;-) But it is more clear this way. Now take a look at the program and ask everything you don't understand.

00 { 90-Byte Prgm }
02 X>Y?
03 X<>Y
04 1E3
05 ÷
06 1
07 +
08 STO 00
09 Rv
10 STO "MAX"
11 SF 01
12 CLA
13>LBL 01
14 RCL 00
15 IP
16 1
17 -
18 RAN
19 RCL× "MAX"
20 IP
21 1
22 +
23 FS?C 01
24 GTO 03
25>LBL 02
27 X=Y?
28 GTO 01
29 Rv
31 GTO 02
32>LBL 03
33 STO IND 00
34 |-" "
35 AIP
36 ISG 00
37 GTO 01
38 RCL "MAX"
39 RCL 00
40 IP
41 1
42 -
43 CLV "MAX"
45 END

Note: "Rv" means R↓ and line 34 appends a space to Alpha.

The following example was obtained after 0,12345 [SEED] on Free42. But I can't say if this will produce the same numbers on the DM-42.

 4 7 3 16
 9 6 17 4
 3 16 4 18


Now, what about adding a sorted output ?-)

Find all posts by this user
Quote this message in a reply
Post Reply 

Messages In This Thread
RE: (DM42) Lottery number generator programming - Dieter - 02-11-2019 07:35 PM

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