📄 283-286.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Using Random Numbers</TITLE>
<!-- BEGIN HEADER --><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><body bgcolor="ffffff" link="#006666" alink="#006666" vlink="#006666"><P>
<CENTER><B>Learn Encryption Techniques with BASIC and C++</B>
<FONT SIZE="-2">
<BR>
<I>(Publisher: Wordware Publishing, Inc.)</I>
<BR>
Author(s): Gil Held
<BR>
ISBN: 1556225989
<BR>
Publication Date: 10/01/98
</FONT></CENTER>
<P>
<!-- Empty Reference Subhead -->
<!--ISBN=1556225989//-->
<!--TITLE=Learn Encryption Techniques with BASIC and C++//-->
<!--AUTHOR=Gilbert Held//-->
<!--PUBLISHER=Wordware Publishing, Inc.//-->
<!--CHAPTER=6//-->
<!--PAGES=283-286//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="280-283.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="286-291.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 6.5</B> The RANDOM1.CPP program listing and its repeated execution using different random seed numbers.</P>
<!-- CODE //-->
<PRE>
/*
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:
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">The RANDOM2.CPP Program</FONT></H4>
<P>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.
</P>
<P>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.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="280-283.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="286-291.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -