Lottery Number Generator
A while ago, I wrote a simple Lottery Number Generator. It may interests you that it has these words in it: "...THEN A MIRACLE OCCURS" Of course, that was done as a joke, but in reality, choosing a bunch of random number isn't rocket science. Especially so, if the programming language provides a built-in random number generator, such as Petit Computer.
All of these talk about the biggest million jackpot ever got me fired up again, and well, I think it's time to update that program. This time, though, I want it to be an actual program that I can use. Speaking of which, there is more than one lottery, so I decided to write a customizable lottery number generator.
- CLS:CLEAR
- PNLTYPE "OFF"
- DIM MN[10]
- DIM MB[10]
- @INIT
- NT=15:'NUMBER OF TICKETS
- NS=2:'NUMBER OF SLOTS
- CS=0:'CURRENT SLOT
- MN[0]=75:'MAXNUM 1-75
- MB[0]=5:'NUMBER OF BALLS
- MN[1]=15
- MB[1]=1
- SE=0:'SELECTOR
- SSL=0:'SAVE SLOT
- DIR=0:'GOSUB @MSL
It's clear that I'm planning to have more than one slot. The old version only has one slot, and this one have more than one. I put in initial value for the most common configuration. Also notice that you can have more than one slot for saving. I do it via MEM$ for saving/loading.
- @LOOP
- VSYNC 1:B=BUTTON(1):TS=TCHST:TT=TCHTIME
- IF TS THEN GOTO @ROLL
- IF B AND 1 THEN SE=SE-1
- IF B AND 2 THEN SE=SE+1
- IF B AND 4 THEN DIR=-1:ON SE GOSUB @MNT,@MNS,@MCS,@MMN,@MMB,@MSL
- IF B AND 8 THEN DIR=1:ON SE GOSUB @MNT,@MNS,@MCS,@MMN,@MMB,@MSL
- IF B AND 64 THEN GOSUB @SAVER
- IF B AND 128 THEN GOSUB @LOADER
That's all for the user interface. There are 6 different actions you can take, and U/D refers to the different action. L/R refers to the value being changed. In truth, there's a simpler 2 dimensional way that you can do this, but I decided to stick with this generic form for now.
- COLOR 15
- PNLSTR 0,0,"LOTTERY GENERATOR"
- PNLSTR 0,1,"(C) 2013 HARRY M. HARDJONO"
- PNLSTR 0,3," NUMBER OF TICKETS: "+STR$(NT)+" "
- PNLSTR 0,4," NUMBER OF SLOTS: "+STR$(NS)+" "
- PNLSTR 0,5," CURRENT SLOT: "+STR$(CS)+" "
- PNLSTR 0,6," MAXNUM 1-"+STR$(MN[CS])+" "
- PNLSTR 0,7," MAXBALL 1-"+STR$(MB[CS])+" "
- PNLSTR 0,8," SAVE NAME:"+MM$+" "
- PNLSTR 0,20," TOUCH SCREEN TO ROLL"
- PNLSTR 0,21,"(X) TO SAVE (Y) TO LOAD"
- PNLSTR 0,23,MID$(MEM$,0,32)
- SE=(SE+6)%6
- PNLSTR 0,SE+3,">"
- GOTO @LOOP
And that's about it for the User Interface. There's really nothing new here. It's just displaying some variables, and that's about it.
- @MNT
- NT=(NT+DIR+21)%21
- RETURN
- @MNS
- NS=(NS+DIR+10)%10
- RETURN
- @MCS
- CS=(CS+DIR+NS)%NS
- RETURN
- @MMN
- MN[CS]=(MN[CS]+DIR+100)%100
- RETURN
- @MMB
- MB[CS]=(MB[CS]+DIR+10)%10
- RETURN
- @MSL
- SSL=(SSL+DIR+10)%10
- MM$="L"+STR$(SSL)+"TTOCFG"
- RETURN
As you can see, the structure is all the same. Therefore, I can very well use a 2 dimensional array to do the whole thing. Doing it this way is actually more complicated, although it does have the benefit of mindless coding. Ha! Mindless coding. Isn't that an oxymoron? Haha, joking aside, it's true that simply adhering to plan is on the low end of the difficulty scale.
- @ROLL
- CLS
- FOR I=0 TO NT-1
- COLOR 15
- ?RIGHT$(" "+STR$(I),2);
- FOR J=0 TO NS-1
- CNL$=""
- IF MB[J]<1 THEN MB[J]=1
- FOR K=0 TO MB[J]-1
- IF MN[J]<1 THEN MN[J]=1
- @THROW
- NUM$=" "+RIGHT$("00"+STR$(RND(MN[J])+1),2)
- IF INSTR(CNL$,NUM$)>=0 THEN GOTO @THROW
- CNL$=CNL$+NUM$
- NEXT K
- COLOR 12+FLOOR((I%10)/5)
- COLOR 4+FLOOR((I%10)/5)
- ? CNL$+" -";
- NEXT J
- ?" "
- NEXT I
- GOTO @LOOP
Then a miracle occurs...
In fact, it's just a simple string search. Everytime I roll a number, the number is checked via INSTR whether or not the it exists in the string. If so, roll another number, else add the number to the string. Pretty simple, yet devious at the same time. Normally, you use an array, associative array, or hash, depending on the language you're using.
- @SAVER
- MEM$=RIGHT$("00"+STR$(NT),2
- MEM$=MRM$+RIGHT$(STR$(NS),1)
- FOR J=0 TO 8:'MAX NS
- MEM$=MEM$+RIGHT$("00"+STR$(MN[J]),2)
- MEM$=MRM$+RIGHT$(STR$(MB[J]),1)
- NEXT J
- SAVE "MEM:"+MM$
- RETURN
- @LOADER
- LOAD "MEM:"+MM$
- NT=VAL(MID$(MEM$,0,2))
- NS=VAL(MID$(MEM$,2,1))
- FOR J=0 TO 8:'MAX NS
- MN[J]=VAL(MID$(MEM$,J*3+3,2))
- MB[J]=VAL(MID$(MEM$,J*3+3+2,1))
- NEXT J
- RETURN
Saving and loading are really just building the string and decoding it. You can see the string on the last line. I made several mistakes here, mainly because I didn't write anything down, and just winging it. I build a long string array because I'm multiplying it by the number of tickets, which is silly because one ticket is the same as the other. Another one is that I multiplied J by 8. Oops. How did that happen?
I was on my bed trying to sleep, so I guess that kind of mistakes can't be helped. Now that I think of it, such mistakes are laughable silly. I guess I shouldn't be programming when I'm trying to sleep. Nah. Who cares? Some people read books on their bed. I'm doing computer programming. That's that.
No comments:
Post a Comment