BBC BASIC
« Lottery numbers & non repeating values tool »

Welcome Guest. Please Login or Register.
Mar 31st, 2018, 10:53pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Cross-platform BBC BASIC (Windows, Linux x86, Mac OS-X, Android, iOS, Raspberry Pi)
BBC BASIC Resources
BBC BASIC Help Documentation
BBC BASIC for Windows Home Page
BBC BASIC Programmers' Reference
BBC BASIC Beginners' Tutorial
BBC BASIC for SDL 2.0 Home Page
BBC BASIC Discussion Group

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Lottery numbers & non repeating values tool  (Read 1136 times)
Leo
New Member
Image


member is offline

Avatar




PM


Posts: 10
smiley Re: Lottery numbers & non repeating values tool
« Reply #6 on: Sep 1st, 2017, 5:19pm »

Here's a very simple version which does multiple lines and outputs numbers in order (without sorting).
Code:
      REM Simple Lottery. Multiple lines, output ordered.      NMAX%=49      LMAX%=7      DIM N&(NMAX%)      @%=&02      INPUT "How many lines", J%      FOR I%=1 TO J%        PROC_Line      NEXT      END      REM Create and print a lottery line      DEF PROC_Line      LOCAL c%, i%, x%      N&()=0      REPEAT        x% = RND(NMAX%)        IF N&(x%) = 0 THEN          N&(x%) = x%          c% += 1        ENDIF      UNTIL c% = LMAX%      FOR i%=1 TO 49        IF N&(i%) THEN PRINT " " i%,;      NEXT      PRINT      ENDPROC 
« Last Edit: Sep 1st, 2017, 7:08pm by Leo » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 803
smiley Re: Lottery numbers & non repeating values tool
« Reply #7 on: Sep 1st, 2017, 8:35pm »

on Sep 1st, 2017, 5:19pm, Leo wrote:
Here's a very simple version which does multiple lines and outputs numbers in order (without sorting).

Personally I dislike and distrust all solutions that rely on repeatedly choosing a 'random' number until it's not one of a previously chosen set (or repeatedly choosing a set of numbers until some condition is met).

I dislike it because the execution time is not predictable. Suppose one was using that technique to draw 52 cards from a pack; the last card drawn is already known (it's the only one left) but a 'repeat RND(52) until not already chosen' loop might have to repeat dozens or hundreds of times before that unique number is selected. Theoretically it could take an indefinitely long time.

I distrust it because it is not obvious 'by inspection' that the method is statistically sound. It probably is, but as I'm not a mathematician I can't be confident that it really is resulting in an unbiased selection (to the degree that the pseudo-random number generator allows, at least).

So I would always prefer a method that has a predictable execution time and guaranteed statistics, such as a well understood and documented shuffling algorithm (e.g. the Fisher-Yates or Knuth shuffle).

Richard.
« Last Edit: Sep 1st, 2017, 8:44pm by Richard Russell » User IP Logged

MarsFS
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 16
smiley Re: Lottery numbers & non repeating values tool
« Reply #8 on: Sep 1st, 2017, 10:20pm »

Here's another version using a knuth shuffle with multiple lines and sorted and formatted output:
Code:
      REM Lotto number generator      max%=49      nums%=7      DIM allNumbers(max%)      DIM sorted(max%)      REM Creat a fresh set of numbers      FOR T%=1 TO max%        allNumbers(T%)=T%      NEXT      REPEAT        PROC_newnumbers        key$ =GET$      UNTIL key$="q" OR key$="Q"      PRINT "Done"      END      DEF PROC_newnumbers      LOCAL T%,N%,C%,J%,A$      CLS      PRINT "Lotto Number Picker"      PRINT "-------------------"      INPUT "How many lines (1-18): ", J%      IF J%<2 OR J%>18 THEN        J%=1        A$="is 1 line"      ELSE        A$="are "+STR$(J%)+" lines"      ENDIF      CLS      PRINT "Here ";A$;" of ";STR$(nums%);" numbers for Lotto Max:": PRINT      FOR C%=1 TO J%        REM randomly shuffle the numbers        FOR T% = max% TO 2 STEP -1          SWAP allNumbers(T%),allNumbers(RND(T%))          sorted(T%)=0        NEXT        sorted(1)=0        REM choose a random set and sort        N%=RND(max%-nums%+1)        FOR T%=N% TO N%+nums%-1          sorted(allNumbers(T%))=allNumbers(T%)        NEXT        REM print sorted list        PRINT "Line ";RIGHT$("0"+STR$(C%),2);":   ";        FOR T%=1 TO max%          IF sorted(T%) PRINT RIGHT$(" "+STR$(sorted(T%)),2);"  ";        NEXT        PRINT      NEXT      PRINT:PRINT "Press any key for more numbers, Press 'Q' to quit"      ENDPROC 

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 803
smiley Re: Lottery numbers & non repeating values tool
« Reply #9 on: Sep 2nd, 2017, 11:15am »

on Sep 1st, 2017, 10:20pm, MarsFS wrote:
Here's another version using a knuth shuffle with multiple lines and sorted and formatted output

Nice, and works without modification on both BB4W and BBCSDL!

For my own interest I ran some tests on how many calls to RND(52) might be necessary before a specific unique number is returned. The answer seemed to be that needing to call it more than 900 times is not that unlikely (the highest I saw was 996)! Obviously typically it will be fewer, but it illustrates the uncertainty in execution time that can arise from using that technique.

Richard.
User IP Logged

Leo
New Member
Image


member is offline

Avatar




PM


Posts: 10
smiley Re: Lottery numbers & non repeating values tool
« Reply #10 on: Sep 2nd, 2017, 4:00pm »

Although I can't disagree with the logic behind "run time is unpredictable" - the lottery case here is a complete frippery. It's just a "throwaway" toy.
Of course I wouldn't use the technique I did for anything approaching any seriousness but I stand by my choice for the lottery case because..

a) It's a complete frippery.
b) The program is very short.
c) We're only selecting ANY 7 numbers from 49 so there's no chance of this taking forever.
d) The quality of the random numbers is irrelevant because any 7 will have an equal chance of winning.

I also ran some trials (a lot).
The maximum number of lookup clashes in any single line was 6 - sounds a lot.
However, in creating 10000 lottery lines a repeat lookup was necessary in 6.7% of cases - no problem.

Henceforward I will use the dates of my children's birthdays to derive (winning) lottery lines. grin
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 803
smiley Re: Lottery numbers & non repeating values tool
« Reply #11 on: Sep 2nd, 2017, 4:43pm »

on Sep 2nd, 2017, 4:00pm, Leo wrote:
I stand by my choice for the lottery case because...

That's fine, with those qualifications. The trouble is that people may adapt the code for other applications without appreciating its limitations (for example they may come upon it years later as the result of a search).

My experience is that there is widespread misunderstanding about pseudo-random numbers and statistics, and although it's unlikely that anybody would use BBC BASIC for an online casino program, or something like that, it's not impossible.

Several years ago I wrote this article at the Wiki to draw attention to some of the pitfalls. It includes simple lottery draw and card shuffling programs. It's out of date to the extent that BBC BASIC no longer uses the value of TIME as a seed.

Richard.
« Last Edit: Sep 2nd, 2017, 4:52pm by Richard Russell » User IP Logged

Leo
New Member
Image


member is offline

Avatar




PM


Posts: 10
smiley Re: Lottery numbers & non repeating values tool
« Reply #12 on: Sep 2nd, 2017, 9:00pm »

Thanks for that link Richard - very good article.
Although I'm gradually working my way through the info in the wiki, I admit I haven't read that one before. Everybody who uses BB4W should go and rummage through the wiki - there's so much excellent information in there.

I use CryptGenrandom a lot for 'professional' use. I think I posted something here that used it a short time ago - but I don't remember what right now. (My memory ain't what it used to be).

Thanks again for the info.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls