Basic language code to generate true random numbers.

BASIC Language Source Code Software to Generate True Random and Unique Numbers

By Ion Saliu, Programmer At-Large

Clear source code for generating random numbers, including for lotto, lottery, Powerball.

First captured by the WayBack Machine (web.archive.org) on October 15, 2011.

RandomNumbers.BAS version 3.01 ~ October 2012 - Software source code for a BASIC language program to generate truly random and unique numbers.


'-- PowerBasic Console Compiler (PBCC) source code to generate truly random and unique numbers.
'-- Accompanied by a function to generate highly randomized seeds: TheSeeder by Ion Saliu.
'-- This source code works accurately without modification in PBCC version 4.04;
'-- the programmer is entirely responsible for transportation to other platforms or PBCC versions.

#DEBUG ERROR ON
OPTION EXPLICIT
DECLARE FUNCTION TheSeeder() AS QUAD
GLOBAL TheSeed AS QUAD
DECLARE SUB SortedCombos()
DECLARE SUB UnSorted()
GLOBAL X() AS LONG
GLOBAL K AS LONG
GLOBAL V AS LONG
GLOBAL GU AS LONG
GLOBAL I AS LONG
GLOBAL j AS LONG
GLOBAL ION AS LONG
GLOBAL S1 AS LONG
GLOBAL S2 AS LONG
GLOBAL VS AS STRING
GLOBAL KS AS STRING
GLOBAL GUS AS STRING
GLOBAL IndexFile AS STRING
GLOBAL Choice AS STRING
GLOBAL Choice1 AS STRING
GLOBAL Choice2 AS STRING
GLOBAL OPT10 AS STRING
GLOBAL AN AS STRING

FUNCTION PBMAIN() AS LONG

MyTop:
'ON ERROR GOTO erpro
COLOR 14, 3
INPUT FLUSH
CURSOR OFF

CLS
LOCATE 1, 5: PRINT "๘ (C)Copyright 1988-2012 Ion Saliu. All rights reserved ๘"
LOCATE 2, 5: PRINT "๘ RandomNumbers.EXE ~ version 3.01 ~ October 2012 - SALIU.COM ๘"
LOCATE 4, 5: PRINT "This program generates combinations of random numbers -"
LOCATE 5, 5: PRINT "N taken M at a time; e.g. 49 lotto numbers, 6 per line."
LOCATE 7, 5: PRINT "There are 2 options:"
LOCATE 9, 5: PRINT "* combinations or numbers sorted in ascending order;"
LOCATE 10, 5: PRINT "** arrangements or unsorted numbers -- like real-life lottery drawings."
LOCATE 12, 5: PRINT "You can also generate one number per line; e.g. roulette: N = 38, M = 1."
LOCATE 14, 5: PRINT "*** The program and its source code are thorouly presented here:"
LOCATE 15, 5: PRINT " saliu.com/random-numbers.html"

LOCATE 18, 2: PRINT "ีออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออธ"
LOCATE 19, 2: PRINT "ณ Select the option: ณ"
LOCATE 20, 2: PRINT "ออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออด"
LOCATE 21, 2: PRINT "ณ S ณ Sorted Combinations ณณ U ณ Unsorted Numbers (Arrangements) ณ"
LOCATE 22, 2: PRINT "ิอออฯออออออออออออออออัอออัออออออออออฯฯอออฯออออออออออออออออออัออออออออออออออออพ"
LOCATE 23, 2: PRINT " ณ X ณ Exit Random-Combination Program ณ"
LOCATE 24, 2: PRINT " ิอออฯออออออออออออออออออออออออออออออออออพ"

WHILE NOT INSTAT: WEND ' Wait for keypress

LOCATE 19, 50: Choice1 = INKEY$: Choice1 = UCASE$(Choice1) 'WAITKEY$
PRINT Choice1

SELECT CASE Choice1

CASE "S"
CALL SortedCombos
GOTO MyTop

CASE "U"
CALL UnSorted
GOTO MyTop

CASE "X"
CLS
CURSOR ON
EXIT FUNCTION

CASE ELSE
BEEP
GOTO MyTop

END SELECT

END FUNCTION


SUB SortedCombos

MyTop:
INPUT FLUSH

CLS
LOCATE 1, 5: PRINT "* Algorithm #1: Sorted Combinations *"
LOCATE 3, 5: PRINT "This function generates combinations of random numbers -"
LOCATE 4, 5: PRINT "N taken M at a time; e.g. 49 lotto numbers, 6 per line."
LOCATE 5, 5: PRINT "** The numbers are SORTED IN ASCENDING ORDER per line **"

LOCATE 7, 10: PRINT " *** Defaults: "
LOCATE 8, 10: PRINT " The biggest number: 49"
LOCATE 9, 10: PRINT " Numbers per combination: 6"
LOCATE 10, 10: PRINT " Total combinations to generate: 100"

LOCATE 14, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 15, 14: PRINT CHR$(186);
LOCATE 15, 64: PRINT CHR$(186)
LOCATE 16, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 15, 15: LINE INPUT " Enter the biggest number -> ", VS
IF LEN(VS)=0 THEN
V=49
ELSE
V=VAL(VS)
END IF

LOCATE 18, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 19, 14: PRINT CHR$(186);
LOCATE 19, 64: PRINT CHR$(186)
LOCATE 20, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 19, 15: LINE INPUT " How many numbers per combination -> ", KS
IF LEN(KS)=0 THEN
K=6
ELSE
K=VAL(KS)
END IF

LOCATE 22, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 23, 14: PRINT CHR$(186);
LOCATE 23, 64: PRINT CHR$(186)
LOCATE 24, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 23, 15: LINE INPUT " How many combinations to generate -> ", GUS
IF LEN(GUS)=0 THEN
GU=100
ELSE
GU=VAL(GUS)
END IF

IF GU<=0 THEN
GU=100
END IF

IndexFile$ = "RndSort" & "-" & MID$(STR$(V),2) & "-" & MID$(STR$(K),2)
Choice$ = ""

CLS
LOCATE 3, 5: PRINT "You can generate the combinations to screen, or -
LOCATE 4, 5: PRINT "save the combinations to a disk file (text format)."
LOCATE 7, 5: PRINT "*** Default output file: "; IndexFile$

LOCATE 10, 23: PRINT "ีอออออออออออออออออออออออออออออออออธ"
LOCATE 11, 23: PRINT "ณ Choose the output device: ณ"
LOCATE 12, 23: PRINT "ฦอออออออออออออออออออออออออออออออออต"
LOCATE 13, 23: PRINT "ณ |S| Screen ณ"
LOCATE 14, 23: PRINT "ฦอออออออออออออออออออออออออออออออออต"
LOCATE 15, 23: PRINT "ณ |D| Disk file ณ"
LOCATE 16, 23: PRINT "ิอออออออออออออออออออออออออออออออออพ"
LOCATE 11, 54: Choice$ = WAITKEY$

Choice$=UCASE$(Choice$): PRINT Choice$
IF Choice$ = "D" THEN
LOCATE 19, 14: PRINT CHR$(201); STRING$(39, 205); CHR$(187)
LOCATE 20, 14: PRINT CHR$(186);
LOCATE 20, 54: PRINT CHR$(186);
LOCATE 21, 14: PRINT CHR$(186);
LOCATE 21, 54: PRINT CHR$(186);
LOCATE 22, 14: PRINT CHR$(200); STRING$(39, 205); CHR$(188)
LOCATE 20, 15: PRINT " Name the output file";
LOCATE 21, 15: PRINT " (";IndexFile;") -> ";: LINE INPUT OPT10
IF OPT10 = "" THEN OPT10 = IndexFile$
OPT10 = UCASE$(OPT10)

OPEN OPT10 FOR OUTPUT AS #3

END IF

'------ generate a highly randomized seed; call TheSeeder function
CLS

FOR I = 1 TO RND(1,1618)
TheSeed = TheSeeder ()
' print TheSeed: waitkey$ ': you can see the random seeds, if you want
NEXT I
RANDOMIZE TheSeed
' print TheSeed: waitkey$ ': you can see the final random seed, if you want

'---- This tiny routine generates unique random numbers to disk = SORTED
IF Choice$ = "D" THEN
FOR ION = 1 TO GU
PRINT "#"; ION; "->";
S2 = K: S1 = V
FOR j = 1 TO S1
IF RND < S2 / S1 THEN PRINT #3, j; : S2 = S2 - 1
S1 = S1 - 1
NEXT j: PRINT #3,
NEXT ION

ELSE
'---- This tiny routine generates unique random numbers to screen = SORTED
FOR ION = 1 TO GU
PRINT "#"; ION; "->";
S2 = K: S1 = V
FOR j = 1 TO S1
IF RND < S2 / S1 THEN
PRINT j;
S2 = S2 - 1
END IF
S1 = S1 - 1
NEXT j: PRINT
NEXT ION

END IF

1201 CLOSE
LOCATE 21, 40: PRINT CHR$(201); STRING$(35, 205); CHR$(187);
LOCATE 22, 40: PRINT CHR$(186);
LOCATE 22, 76: PRINT CHR$(186);
LOCATE 23, 40: PRINT CHR$(186);
LOCATE 23, 76: PRINT CHR$(186);
LOCATE 24, 40: PRINT CHR$(200); STRING$(35, 205); CHR$(188);
LOCATE 22, 41: PRINT " Combinations generated: "; GU
LOCATE 23, 41: PRINT " Run the function again (Y/N)?"

AN$ = "": AN$ = WAITKEY$
IF AN$ = "n" OR AN$ = "N" THEN
EXIT SUB
ELSE
ON ERROR GOTO 0
GOTO MyTop
END IF

END SUB


SUB UnSorted

MyTop:
INPUT FLUSH
CLS

LOCATE 1, 5: PRINT "* Algorithm #2: UnSorted Numbers (Arrangements) *"
LOCATE 3, 5: PRINT "This function generates combinations of random numbers -"
LOCATE 4, 5: PRINT "N taken M at a time; e.g. 49 lotto numbers, 6 per line."
LOCATE 5, 5: PRINT "** The numbers are UNSORTED -- like real lottery drawings **"

LOCATE 7, 10: PRINT " *** Defaults: "
LOCATE 8, 10: PRINT " The biggest number: 49"
LOCATE 9, 10: PRINT " Numbers per combination: 6"
LOCATE 10, 10: PRINT " Total combinations to generate: 100"

LOCATE 14, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 15, 14: PRINT CHR$(186);
LOCATE 15, 64: PRINT CHR$(186)
LOCATE 16, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 15, 15: LINE INPUT " Enter the biggest number -> ", VS
IF LEN(VS)=0 THEN
V=49
ELSE
V=VAL(VS)
END IF

LOCATE 18, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 19, 14: PRINT CHR$(186);
LOCATE 19, 64: PRINT CHR$(186)
LOCATE 20, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 19, 15: LINE INPUT " How many numbers per combination -> ", KS
IF LEN(KS)=0 THEN
K=6
ELSE
K=VAL(KS)
END IF

LOCATE 22, 14: PRINT CHR$(201); STRING$(49, 205); CHR$(187)
LOCATE 23, 14: PRINT CHR$(186);
LOCATE 23, 64: PRINT CHR$(186)
LOCATE 24, 14: PRINT CHR$(200); STRING$(49, 205); CHR$(188)
LOCATE 23, 15: LINE INPUT " How many combinations to generate -> ", GUS
IF LEN(GUS)=0 THEN
GU=100
ELSE
GU=VAL(GUS)
END IF

IF GU<=0 THEN
GU=100
END IF

IndexFile$ = "RndUnSort" & "-" & MID$(STR$(V),2) & "-" & MID$(STR$(K),2)
Choice2$ = ""
CLS

LOCATE 3, 5: PRINT "You can generate the combinations to screen, or -
LOCATE 4, 5: PRINT "save the combinations to a disk file (text format)."
LOCATE 7, 5: PRINT "*** Default output file: "; IndexFile$

LOCATE 10, 23: PRINT "ีอออออออออออออออออออออออออออออออออธ"
LOCATE 11, 23: PRINT "ณ Choose the output device: ณ"
LOCATE 12, 23: PRINT "ฦอออออออออออออออออออออออออออออออออต"
LOCATE 13, 23: PRINT "ณ |S| Screen ณ"
LOCATE 14, 23: PRINT "ฦอออออออออออออออออออออออออออออออออต"
LOCATE 15, 23: PRINT "ณ |D| Disk file ณ"
LOCATE 16, 23: PRINT "ิอออออออออออออออออออออออออออออออออพ"

LOCATE 11, 54: Choice2$ = WAITKEY$
Choice2$=UCASE$(Choice2$): PRINT Choice2$

IF Choice2$ = "D" THEN
LOCATE 19, 14: PRINT CHR$(201); STRING$(39, 205); CHR$(187)
LOCATE 20, 14: PRINT CHR$(186);
LOCATE 20, 54: PRINT CHR$(186);
LOCATE 21, 14: PRINT CHR$(186);
LOCATE 21, 54: PRINT CHR$(186);
LOCATE 22, 14: PRINT CHR$(200); STRING$(39, 205); CHR$(188)
LOCATE 20, 15: PRINT " Name the output file";
LOCATE 21, 15: PRINT " (";IndexFile;") -> ";: LINE INPUT OPT10
IF OPT10 = "" THEN OPT10 = IndexFile$
OPT10 = UCASE$(OPT10)

OPEN OPT10 FOR OUTPUT AS #3

END IF

'------ generate a highly randomized seed; call TheSeeder function
CLS

FOR I = 1 TO RND(1,1618)
TheSeed = TheSeeder ()
' print TheSeed: waitkey$ ': you can see the random seeds, if you want
NEXT I
RANDOMIZE TheSeed
' print TheSeed: waitkey$ ': you can see the final random seed, if you want

REDIM X(V)

FOR I = 1 TO V
X(I) = I
NEXT I

FOR ION = 1 TO RND(2, 10)
FOR I = 1 TO V - 1
SWAP X(I), X(INT(RND * (V - I + 1) + I))
NEXT I
NEXT ION

'---- This tiny routine generates unique random numbers to disk = unSORTED
IF Choice2 = "D" THEN

FOR ION = 1 TO GU

PRINT ION; "->";

FOR I = 1 TO V - 1
SWAP X(I), X(INT(RND * (V - I + 1) + I))
NEXT I

FOR J = 1 TO K
PRINT #3, X(J);
NEXT J
PRINT #3,

NEXT ION

'---- This tiny routine generates unique random numbers to screen = unSORTED
ELSE

FOR ION = 1 TO GU

PRINT ION; "->";

FOR I = 1 TO V - 1
SWAP X(I), X(INT(RND * (V - I + 1) + I))
NEXT I

FOR J = 1 TO K
PRINT X(J);
NEXT J
PRINT

NEXT ION

END IF

1201 CLOSE

ERASE X()

LOCATE 21, 40: PRINT CHR$(201); STRING$(35, 205); CHR$(187);
LOCATE 22, 40: PRINT CHR$(186);
LOCATE 22, 76: PRINT CHR$(186);
LOCATE 23, 40: PRINT CHR$(186);
LOCATE 23, 76: PRINT CHR$(186);
LOCATE 24, 40: PRINT CHR$(200); STRING$(35, 205); CHR$(188);
LOCATE 22, 41: PRINT " Combinations generated: "; GU
LOCATE 23, 41: PRINT " Run the function again (Y/N)?"

AN$ = "": AN$ = WAITKEY$
IF AN$ = "n" OR AN$ = "N" THEN
EXIT SUB
ELSE
ON ERROR GOTO 0
GOTO MyTop
END IF

END SUB


'----- Highly randomized seed generation: from hundreds of thousands, to millions, to billions
'----- Function created by Ion Saliu -- no more constant seeds, such as TIMER by itself

FUNCTION TheSeeder() AS QUAD

DIM TheSeed AS QUAD
DIM Seed1 AS QUAD
DIM Seed2 AS QUAD
DIM This AS STRING
DIM Num1 AS DWORD
DIM Num2 AS DWORD
DIM Num3 AS DWORD

' -- use system date and time to get seeds different from day to day
RANDOMIZE (TIMER + CLNG(RND*TIMER+1) + CLNG(RND*TIMER+1) * CLNG(RND*TIMER+1))

This = DATE$

Num1 = VAL(MID$(This, 1, 2))
Num2 = VAL(MID$(This, 4, 2))
Num3 = VAL(MID$(This, 7, 4))

Seed1 = (CQUD(RND * Num1) + 1) * (CQUD(RND * Num2 + 2) * (CQUD(RND * Num3 + 3) + 4))
Seed2 = (CQUD(RND * Seed1) + 5) * Seed1
TheSeed = Seed2 + (CQUD(RND * TIMER + 1) * CQUD(RND * Num3 + Num2))

TheSeeder = TheSeed

END FUNCTION


The compiled program looks like this:

The compiled program of Basic random number generator for any lotto games.

True random number generators: The most randomly random numbers.