62.

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


Listing 6.5 The RANDOM1.CPP program listing and its repeated execution using different random seed numbers.

 /* random1.cpp C++ code written by Jonathan Held on April 26, 1998, using Microsoft Visual C++ version 5.0 Author's note: In C++, if you wish to randomize without having to enter a seed each time, you can use the statement:  srand(time(NULL)); This causes the computer to read its clock to obtain the value for the seed automatically.  The time function (found in the library time.h) returns the current "calendar time" in seconds. */ #include <iostream.h> #include <stdlib.h> //function prototypes unsigned getSeed(void); void generateNumbers(const unsigned); //---------------------------------------------------------------- //Function: main() //Parameters: None //Return Type: int - 0 if program terminated normally //Purpose: driver that makes calls to various procedures defined //in random1.cpp //---------------------------------------------------------------- int main(){   unsigned seed;   while (seed = getSeed())    generateNumbers(seed);  return 0; }//end main() //---------------------------------------------------------------- //Function: getSeed() //Parameters: none //Return Type: unsigned int - the seed the client entered //Purpose: gets the seed for the random number generator.  If the //user enters a negative number, it causes the seed to underflow to //a very high non-negative number. //---------------------------------------------------------------- unsigned getSeed(){  unsigned seed_Number;   cout << "Enter seed, 0 to terminate: ";   cin >> seed_Number;  return seed_Number; }//end getSeed() //---------------------------------------------------------------- //Function: generateNumbers() //Parameters: unsigned int - the seed the client entered //Return Type: None //Purpose: Generates random numbers between 1 and 100 and prints //only those numbers that are less than 25.  Note that if you //enter the same seed number at different times, you get the same //"random" number sequence.  Using the srand(time(NULL)) will //lessen the chance that the same seed is used twice to generate //random numbers. //---------------------------------------------------------------- void generateNumbers(const unsigned SEED){  int ran_num;  srand(SEED);  for (int ix=0; ix < 75; ix++){    ran_num = 1 + rand() % 100;    if (ran_num < 25)       cout << ran_num << " ";   }   cout << endl << endl;   return; }//end generateNumbers() //end file random1.cpp A:\c>random1 Enter seed, 0 to terminate: 5 15 22 8 8 18 12 23 11 16 23 7 16 20 16 13 6 5 18 2 11 7 Enter seed, 0 to terminate: 7 23 16 22 17 19 10 2 16 13 13 2 14 4 12 20 18 20 4 20 20 Enter seed, 0 to terminate: 9 10 23 12 18 3 13 17 18 2 5 11 20 9 5 15 22 Enter seed, 0 to terminate: 

The RANDOM2.CPP Program

Continuing our exploration of C++ and random numbers, the RANDOM2.CPP program listing is shown in the top portion of Listing 6.6. This program is equivalent to RANDOM2.BAS to encode your message using random numbers. The lower portion of Listing 6.6 illustrates several examples of the execution of the program. When comparing the results to the results obtained by the execution of RANDOM2.BAS, you will note that the use of the same seed values produces different results based upon differences between the two programs in their built-in random number generators. As you will note, RANDOM2.CPP uses random numbers greater than 25 and adds that number to the location of each character in the plaintext array. The sum is then divided by 26 in the same manner as RANDOM2.BAS.

Both RANDOM1.CPP and RANDOM2.CPP and their executable files are contained on the CD-ROM accompanying this book. The executable versions are named RANDOM1.EXE and RANDOM2.EXE. All four programs are located in the C directory on the CD-ROM.


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