True random numbers.

BASIC source code software to generate true random & unique numbers

By Ion Saliu, Programmer At–Large

Power Basic Console Compiler source code to generate truly random & unique numbers.

Written by Ion Saliu on June 18, 2004 (4 WE).

• RandomNumbers.BAS version 1.0 - June 2004 (4 WE) - Freeware source code for a BASIC language programme to generate truly random and unique numbers.

There are four types of sets: exponents, permutations, arrangements, and combinations. I assume the original poster wanted to generate random combinations or random numbers for lottery (lotto combinations). In the case of the combination set, the random numbers are unique (per line) and also sorted in ascending order (per line). You can read a lot more about sets, and also get freeware written in PowerBasic Console Compiler:
Combinatorics: Calculate, generate exponents, permutations, combinations - for any numbers and words.

The following is a fully functional program that runs in PowerBasic IDE. The source code uses a little-known algorithm of generating unique random numbers. The numbers are truly random. The randomization seed is highly random; therefore the next sequence of (set) of numbers is unpredictable. The algorithm in this sample program is one of the most concise algorithms of random number generation. There are numerous algorithms to generate random numbers or combinations. My software, for example, uses a dozen or so algorithms (routines) to generate random numbers. NOT — they are not available!

The computers can generate numbers as highly random as by manual selection or running mechanical devices. The first essential condition is the random seed. Using the computer timer results in lowly random generation. They call it pseudo-randomness. I did write far better seed generators for Visual Basic and PowerBasic. The second essential condition for true random number generation is the SPEED of execution. The higher the speed, the lower the degree of randomness. The software takes an 18-digit floating-point number and generates the first random number (which is floating point, between 0 and 1). The software multiplies that number between 0 and 1 by the largest number in a lotto game (as an example). The calculation is very fast. The software generates quickly another floating-point random number and multiplies it again, etc. Because the calculations are extremely fast, the numbers generated tend to be close to their neighbors. I remember way back when I used an Atari (very slow!) to generate random numbers. The degree of randomness was far higher than what the much faster PCs gave me. Now, delaying the random generation does improve the degree of randomness. My lottery software is not plagued by computerized pseudo-randomization because it performs up to billions of operations. The operations delay considerably the generating of combinations. I noticed long ago that if the seed generation is inside the generating loop and there is no delay, the numbers tend to repeat or to be very close to each other. For, example generating randomly 6 lotto numbers from 1 to 49:

              FOR Counter = 1 to 6
         RANDOMIZE TheSeed
              Lottonumber(Counter) = CLNG(RND * 49)  + 1
               NEXT Counter

But if I add a DELAY command just before the NEXT Counter command, then the randomization improves. Also the RANDOMIZE TheSeed command should be moved outside the loop (before FOR Counter = 1 to 6). Slowing down the generating procedure assures satisfactory random numbers. Don't take randomness as being perfect. Randomness is random — that's all! The delay in execution is also acceptable in this algorithm. The program divides the two random numbers and the result is compared to the RND result. The delay is just sufficient to make the numbers (lotto combinations) acceptably random. Nobody will be able to predict accurately the next sequence of numbers.

And here is one more high-prized randomizing algorithm of mine. The algorithm very much resembles shuffling a deck of cards.

Again, 100% compatible with PowerBasic Console Compiler (PBCC)…

DIM N AS LONG     'total elements in the set to be shuffled 
' e.g. N = 52 cards or N = 49 lotto balls
DIM X() AS LONG   'create an array with all the elements from 1 to N

REDIM X(N)

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

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

The new X(N) is pretty well shuffled after just one iteration.

Included is a highly randomized function to set the random seed: TheSeeder. It uses the system date and time, instead of just the TIMER. Many confuse randomness for probability. If the seed is between 1 and 10, for example, the seed is not considered to be random. In fact, what we talking about is the DEGREE OF RANDOMNESS. The larger the range of the seed, the more randomized the seed is. That's so because the probability of selecting one particular seed is lower. The TIMER takes 86400 values (number of seconds in 24 hours). Of course, if using the TIMER only as the seed and running the random generator at the same time of the day, the sequence of numbers will be always the same. Hence, the term pseudo-random. TheSeeder() generates random seeds between millions and billions. You won't see the same seed again in your lifetime!
You can read a lot more about randomness: Almighty Number, Randomness, Universe, God, Order, History, Repetition.

Hope you'll find the Basic source code useful, albeit a little long. No error trapping was implemented. To avoid errors, make both parameters positive. The biggest "lotto" number must be larger than “numbers per combination”. If the two parameters are equal, one and only one sequence of randomly-generated numbers will be generated (eg 1 2 3 4 5 6). The code requires changes when ported to other Basic implementations or other programming languages. That would be entirely your job, in case you speak a different programming language...

•• You can also download the compiled random number program, which is more potent: It adds a function that delays the random generation of numbers. The software mimics a lotto drawing conducted by the state lotteries. Program name: RandomNumbers.EXE. Software categories: 5.2 & 5.6 - accessible via the main download page.

The fully functional program can also generate roulette spins. The parameters: Largest number 37 (French roulette) or 38 (American roulette); numbers per combination = 1 (one spin at a time). Then deduct 1 from the outcome. Thus, roulette number 1 becomes 0, while 37 becomes 36. The special case in American roulette: number 38 becomes double zero (00).

OK, okay! There is also the question of verification. How can we verify that the numbers are truly random? Good question!

All events are random -- only the degree of randomness is variable. We can verify true randomness by applying Ion Saliu's Paradox of N Trials. A total of {1 - 1/e} or 63% numbers are unique. Thus, if we generate N numbers, around 37% of them will be duplicates or repeats. For example, if we generate 37 roulette numbers, around 25 of them will be unique or non-repeats; 12 or so of the roulette numbers will be repeats from previous spins.


'------- Power Basic Console Compiler source code to generate truly random & unique numbers
OPTION EXPLICIT
DECLARE FUNCTION TheSeeder () AS QUAD
GLOBAL K AS LONG
GLOBAL V AS LONG
GLOBAL GU AS LONG
GLOBAL j AS LONG
GLOBAL ION AS LONG
GLOBAL S1 AS LONG
GLOBAL S2 AS LONG
GLOBAL TheSeed AS EXT
GLOBAL VS AS STRING
GLOBAL KS AS STRING
GLOBAL GUS AS STRING
GLOBAL IndexFile AS STRING
GLOBAL Choice 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
PAGE 2,1
CLS
LOCATE 1, 1: PRINT "ψ (C)2004 Ion Saliu. All rights reserved www.saliu.com  ψ"
LOCATE 2, 1: PRINT "ψ RandomNumbers.EXE ψ version 1.0, June 2004 (4 WE) ψ"
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, 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)
PCOPY 2,1
PAGE 1,1
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$ = "Comb" & MID$(STR$(V),2) & "-" & MID$(STR$(K),2) & ".NR"
Choice$ = ""
PAGE 3,1
CLS
LOCATE 3, 5: PRINT "You can generate the combinations to screen, or -
LOCATE 4, 5: PRINT "save the combinations to disk."
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 "ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ"
PCOPY 3,1
PAGE 1,1
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
'PRINT : PRINT "  "; K
        END IF
CLS
'------ generate a highly randomized seed; call TheSeeder function
TheSeed=TheSeeder
RANDOMIZE TheSeed
'---- This tiny algorithm generates unique random numbers to disk
 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 function generates unique random numbers to screen
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
     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 program again (Y/N)?"
  AN$ = "": AN$ = WAITKEY$
      IF AN$ = "n" OR AN$ = "N" THEN
          CURSOR ON
          EXIT FUNCTION
          ELSE
          ON ERROR GOTO 0
          GOTO MyTop
      END IF

END FUNCTION

'----- highly randomized seed generation: from millions to billions
FUNCTION TheSeeder() AS QUAD
DIM TheSeed AS QUAD
DIM Seed1 AS QUAD
DIM Seed2 AS QUAD
DIM This AS STRING
DIM Num1 AS LONG
DIM Num2 AS LONG
DIM Num3 AS LONG
RANDOMIZE TIMER
' --- use system date and time to get seeds different from day to day
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


Randomness is the reason why God fears Mathematics, while Einstein hates gambling.

Only Randomness is Almighty. Randomness is great!

BASIC language source code to generate true random numbers.

Ioan Axiomaticus Salius,
Doctor in Occult Science of Numerica Aleatora
('Random Numbers Science'; derived from Julius Caesar's outpour of joy at glancing a big gamble win — the Gallic Wars — alea iacta est; some first-impression translations sound like "pe alee este un iaht"; or "a yacht on the east alley"; both wrong; always plunge deeper in the sea of knowledge!)

True random number generators: The most randomly random numbers.



Random number, numbers generator: Basic source code, algorithm, function.
Google
 

Subject:

Comments:




Generate rue random numbers in computer software.

Socrates, Philosophy, Science, Software, Gambling, Lotto, Lottery. Socrates Home  Search Web Site, Internet Web Encyclopedias. Search  Help on Software, Gambling, Lotto, Lottery. Ion, Help!  Lotto, Lottery, Gambling Software, Systems: Contact Information. Contact Ion  Free, Buy: Lotto, Lottery Software, Gambling Systems. What's New  Gambling Formula: Probability Theory, Mathematics. Fundamental Formula of Gambling  Statistics, probability: Formulae, software, formulas. Statistics Software  Standard Deviation, deviation standard. Standard Deviation  Lotto, Lottery, Software, Systems, Wheels. Lottery, Lotto, Software, Systems  Lotto, Lottery Software, Excel Spreadsheets. Spreadsheets, Lotto, Lottery  Blackjack, Counting Card, Software, Blackjack Basic Strategy. Blackjack Strategy  Roulette, Ruleta, Software. Roulette Systems  Horse races, horse racing. Horse Racing Software  Sports betting, football, software, spreadsheet. Sports Betting, Football, Software  Baccarat Systems, Strategy, Software. Baccarat Theory, Software  Ion Saliu's Book of Records in History. Ion Saliu's Book of Records  Odds Calculator, Random Numbers Generator. Random Generator, Odds Calculator  Message Board, Forum: Lotto, Loto, Lottery, Gambling. Newest Writings  Download Software: Lotto, Lottery, Gambling. Download Free Software  Content, Categories, Links, Resources, Web Pages. Content Categories  Sitemap: Lotto, lottery, gambling, roulette, blackjack, software. All Pages, Sitemap

SALIU.COM is the greatest site in random numbers generating.

Copyright ©1998-2008, Ion Saliu. All rights reserved worldwide. Reproduction, in any form, of the contents of this site is strictly prohibited. Read important copyright information regarding web site www.saliu.com Meta random