HP Forums
(DM42) Lottery number generator programming - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not quite HP Calculators - but related (/forum-8.html)
+--- Thread: (DM42) Lottery number generator programming (/thread-12400.html)



(DM42) Lottery number generator programming - psw - 02-10-2019 09:54 AM

Hello,

I am a sheer beginner of DM42 programming and interesting in a simple and useful program. Today I conceive something like the random number generator and show sets of integers on DM42/Free42 or HP-42S. In short, Linux command:
Code:
shuf -i 1-Y -n X
but I can't translate some parts.

By the way, this program would suggest horse racing Trio BOX or Lotto 7 (which I don't buy them so often.)
I wish you could help me to improve this idea.

Sample select 4 nominations from 18 horses. Y=18 X=4

--- X and Y register into R0 and R1
STO 00
R↓
STO 01

--- Random Number Part to store into Alpha memory
LBL "RNG"
DATE
TIME
x
SEED
RAN
RCLx 01
IP

-- Verify given numbers are not the same each other. (This is going to mess my brain)
x=y?
R↓
ASTO
GTO "RNG"

---Loop for X times
LBL "LOOP"
1
STO- 00
RCL 00
X≤0?
RTN
AVIEW
END

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.

Best regards,


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

(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:

Code:
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.

Code:
00 { 90-Byte Prgm }
01>LBL "RANDOM"
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
26 RCL IND ST Y
27 X=Y?
28 GTO 01
29 Rv
30 DSE ST Y
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"
44 AVIEW
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.

18 [ENTER] 4 XEQ"RANDOM"
 4 7 3 16
[R/S]
 9 6 17 4
[R/S]
 3 16 4 18

etc.

Now, what about adding a sorted output ?-)

Dieter


RE: (DM42) Lottery number generator programming - Thomas Klemm - 02-12-2019 12:57 AM

(02-11-2019 07:35 PM)Dieter Wrote:  Now, what about adding a sorted output ?-)

The following program uses the bits of an integer to simulate a set.
Using the BIT? function allows to detect a duplicate value.
It then goes back to label 00 and tries again.

Code:
00 { 54-Byte Prgm }
01▸LBL "RANDOM"
02 STO 00
03 0
04 X<>Y
05▸LBL 00
06 R↓
07▸LBL 01
08 RAN
09 RCL× ST Z
10 IP
11 BIT?
12 GTO 00
13 2
14 X<>Y
15 Y↑X
16 +
17 DSE 00
18 GTO 01
19 X<>Y
20 1
21 -
22 1ᴇ3
23 ÷
24▸LBL 02
25 BIT?
26 VIEW ST X
27 ISG ST X
28 GTO 02
29 END

The limitation however is that this works only up to 35 horses.

Example:

18
ENTER
4
XEQ "RANDOM"

ST X=6.017
R/S
ST X=10.017
R/S
ST X=15.017
R/S
ST X=16.017
R/S
18.017

Make sure to have the printer off (POFF) and flag 21 (Printer Enable) set, i.e. SF 21.
Otherwise the VIEW command in line 23 will not stop.

Have fun with your DM42.
Thomas


RE: (DM42) Lottery number generator programming - psw - 02-12-2019 05:56 AM

Thank you very much, Dieter and Thomas, how grateful your help are.

To Dieter, your explanation is very clear and handy, digesting a lot of complicated images into the beginner's brains. This 41/42 programming is a little similar to BASIC and C for the loop control. I am very happy to know the counter functions are bundled and do not need to use registers. And it also tells me that I need to study all the functions do what in this machine.
Quote:01>LBL "RANDOM"
02 X>Y?
03 X<>Y
First, I am sort of impressed like a lightning into my eyes. This small idea is unexpectedly useful for someone who doesn't know which number to put X register. It makes user friendly and some non-tech savvy people can get the right numbers without errors. This tells me to use my brain more flexible. I will try to do.
The concept that RAN x Y results 0 to Y-1, came up my mind, short after trying RAN function without programming. That's my bad.
Quote:04 1E3
05 ÷
06 1
07 +
08 STO 00
Second, I wondered why you made a change of X register into 1.004, puzzled a lot and finally figured out this is for the Loop control DSE and ISG indirectly (so the registers don't mess up by loop counter), also I was amazed that the way of LOOP work to reduce memory into just ONE stack value for a loop. Therefore this HP 41 base series was used by NASA in 80's. SetFlag and FlagChech?Clear is as C language
Code:
if (i==0) do {next program} else {skip next program}; i=0
something like this. Very important to put a separator between two number in the alpha memory. I need to study a lot about loop control, I guess. And the sorting these numbers are very pleasant to read the result. But I didn't come up with the single idea. Please do teach me, this was related with the flowchart flaws of X=Y? doesn't really help to verify all the numbers are unique.

FYI, input of
Quote: 0.12345 [SEED] 18 [ENTER] 4 [EXQ] "RANDOM"
to my DM42 returns
5 14 4 6
[R/S]
12 3 14 14
[R/S]
1 12 10 3
Perhaps, random generator is different from Free42.

To Thomas, yes they are my flowchart flaws. The first one; "the numbers may not be unique each other" issue, I noticed this early but couldn't really find the way to do the comparison. Now, what BIT? does is still not clear in my brain but the hint "this may detect up to 35 horses", thus this machine operate 36 bits in BASE mode, my friend has an idea about binary numbers but unfortunately I don't have much, now I understand this program can't be used for a lottery which the numbers are usu more than 35.
The second concern the AVIEW could be very long, well, for lottery buyers need 6 or 7 numbers, for horse ticket buyers wouldn't be crazy and it used to be many horses at one race, these days 18 is maximum gate number in Japan and 20 or so in France.

So, the name of the program should be "horseracing ticket picker" and the number limit MAX = 35 and 6 drawings due to the printable capacity of AVIEW at glance.
Will you add this collection to the DM42/Free42 program library? It can be used as a dice roller, if they don't watch horse races. I'd like to think and hear what would Dieter and Thomas develop the program together.

Best regards,