60.

Learn Encryption Techniques with BASIC and C++
(Publisher: Wordware Publishing, Inc.)
Author(s): Gil Held
ISBN: 1556225989
Publication Date: 10/01/98

Previous Table of Contents Next


RANDOMIZE Statement

The use of an easily duplicated random number sequence clearly limits its usefulness as an encryption and decryption key. Fortunately, BASIC contains a statement that can be used to reseed the random number generator and produce a different sequence of random numbers. The statement that reseeds the random number generator is the RANDOMIZE statement whose format is RANDOMIZE[expression]. Note that the random number sequence uses the seed to generate a new sequence of random numbers, with the first number becoming a new seed. That new seed is then used to generate a newer seed, and so on, based upon the algorithm used by BASIC to generate random numbers. Thus, the seed generated by the use of the RANDOMIZE statement governs the location where the random number generation process begins.

If the optional expression in the RANDOMIZE statement is omitted, BASIC will pause and display the message “Random Number Seed (-32768 to 32767)?” To change the sequence of random numbers generated by the use of a RND function, you can place a RANDOMIZE statement at the beginning of a program. Listing 6.2 illustrates this concept, with the top portion containing a small program listing that includes the RANDOMIZE statement, while the lower portion shows the results of executing the program three times. Note that the use of a different seed results in the generation of a new random number sequence.

Listing 6.2 Using the RANDOMIZE statement.

 RANDOMIZE FOR I = 1 TO 5 PRINT RND(I); NEXT I PRINT Random-number seed (-32768 to 32767)? -5 .4362451       .3257061.2495473.1388514.1759562 Random-number seed (-32768 to 32767)? -5 .4362451       .3257061.2495473.1388514.1759562 Random-number seed (-32768 to 32767)? 0 .7641413       .3576428.1068624.70753124.804176E-02 

Because the value of the random number seed can range from -32678 to 32767, you can obtain the ability to generate 65,536 different random number sequences through the use of the RANDOMIZE statement. You can consider using a variable with a RANDOMIZE statement. Reading the value for the variable from the keyboard serves as a mechanism to provide a program user with the ability to specify the random number sequence to be used to encipher and decipher messages. The seed value then becomes the “code” or “key” for the enciphering and deciphering process.

Working with Random Numbers

Because a majority of the applications for random numbers are for numbers in some range other than 0 to 1, you need a mechanism to change those numbers to integers. That mechanism is obtained by multiplying the value of RND by 10n, where n defines the number of integers you need from 1 to 6. You can then use the BASIC INT function to obtain the integer value of the resulting computation. For example, INT(RND*100) would produce integers whose values are between 0 and 99, while INT(RND*1000) would produce integers whose values are between 0 and 999.

The RANDOM1.BAS Program

The top portion of Listing 6.3 contains the program listing of RANDOM1.BAS. The lower portion shows the results obtained when the program is executed three times. This program generates random numbers between 0 and 25 based upon the use of the RND function and reseeds the random number generator by specifying a reseeding position. You should note that the program simply discards any number exceeding 25 to smooth the resulting random numbers to the range 0 to 25.

Listing 6.3 The RANDOM1.BAS program listing and sample execution with different values assigned to the RANDOMIZE statement.

 REM Program RANDOM1.BAS REM routine to generate random numbers between 0 and 25        CLS start: INPUT "Enter a number, 0 to terminate: ", y        IF y = 0 THEN STOP        RANDOMIZE (y)        FOR I = 1 TO 75        x = INT(RND(I) * 100)        IF x > 25 THEN GOTO skip        PRINT x; skip:  NEXT I        PRINT        GOTO start Enter a number, 0 to terminate: 5 23 7 19 18 22 6 17 15 1 3 17 23 20 13 14 Enter a number, 0 to terminate: 7 11 5 16 20 16 3 15 4 4 23 4 4 24 23 21 19 8 3 11 6 3 Enter a number, 0 to terminate: 9 1 14 14 22 7 22 7 19 23 4 20 9 2 23 5 15 11 Enter a number, 0 to terminate: 0 


Previous Table of Contents Next


Learn Encryption Techniques with Basic and C++
Learn Encryption Techniques with BASIC and C++
ISBN: 1556225989
EAN: 2147483647
Year: 2005
Pages: 92
Authors: Gil Held

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net