Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language: C++ #include \"stdafx.h\" #include <iostream> #include <io

ID: 3863535 • Letter: P

Question

Programming Language: C++

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <random>

using namespace std;

const int MAX_WORDS = 10;

string Noun_1[MAX_WORDS];       // person, place or thing
string Noun_2[MAX_WORDS];       // person, place or thing
string Noun_3[MAX_WORDS];       // person, place or thing
string Noun_S[MAX_WORDS];       // person, place or thing, ending in the letter 's'
string Verb_1[MAX_WORDS];       // action
string Verb_2[MAX_WORDS];       // action
string Verb_ED[MAX_WORDS];       // action, ending in the letters 'ed'
string Adverb[MAX_WORDS];       // descriptive of a verb
string Adjective_1[MAX_WORDS];   // modifyer
string Adjective_2[MAX_WORDS];   // modifyer
string Adjective_3[MAX_WORDS];   // modifyer
string Color[MAX_WORDS];       // red, green, etc.
string Relative[MAX_WORDS];       // person
string School[MAX_WORDS];       // place
string Time[MAX_WORDS];           // time of day, any format

int _tmain(int argc, _TCHAR* argv[])
{
  
  
   // randome number generator, run these three lines once at the beginning of the program
   random_device rd;   // non-deterministic generator
   mt19937 gen(rd()); // to seed mersenne twister.
   uniform_int_distribution<> dist(0,9); // distribute results between 0 and 9 inclusive.
  
   dist(gen); // insert dist(gen) everywhere you need a random number between 0 and 9

/*
Dear <Relative>,

   I am having a(n) <Adjective_1> time here at <School>. My teachers are all <Adjective_2> and I am
doing quit <Adjective_3>. Yesterday, I <Verb_ED> all day. It was <Adverb> <Verb_1> all afternoon but
changed to <Color> by <Time>. I met a new <Noun_1>. I call him <Name_2> and we <Verb_2> every
day. Please send <Noun_2> to help pay for <Noun_S>.

Love <Name_3>.
*/


   system("Pause");
   return 0;
}

Complete the project.

Insert the necessary code to complete the project as specified in the Background above.

Included in the given code is a sample of how to generate a random number from 0 to 9 that you will need to use to select your array index.

The text between the comment characters /* and */ should be the basis of your output. Substitute the array names and indexes for the names within the < and > characters.
Test your code.

Run the program several times to insure that the random number generator is selecting values from the arrays at random.

Explanation / Answer

introduction to the random number functions that come as part of the C++ standard library, namely rand() and srand().

rand() and RAND_MAX

The C++ standard library includes a pseudo random number generator for generating random numbers. In order to use it we need to include the <cstdlib> header. To generate a random number we use the rand() function. This will produce a result in the range 0 to RAND_MAX, where RAND_MAX is a constant defined by the implementation.

Here's a piece of code that will generate a single random number:

The value of RAND_MAX varies between compilers and can be as low as 32767, which would give a range from 0 to 32767 for rand(). To find out the value of RAND_MAX for your compiler run the following small piece of code:

srand()

The pseudo random number generator produces a sequence of numbers that gives the appearance of being random, when in fact the sequence will eventually repeat and is predictable.

We can seed the generator with the srand() function. This will start the generator from a point in the sequence that is dependent on the value we pass as an argument. If we seed the generator once with a variable value, for instance the system time, before our first call of rand() we can generate numbers that are random enough for simple use (though not for serious statistical purposes).

In our earlier example the program would have generated the same number each time we ran it because the generator would have been seeded with the same default value each time. The following code will seed the generator with the system time then output a single random number, which should be different each time we run the program.

Don't make the mistake of calling srand() every time you generate a random number; we only usually need to call srand() once, prior to the first call to rand().

Generating a number in a specific range

If we want to produce numbers in a specific range, rather than between 0 and RAND_MAX, we can use the modulo operator. It's not the best way to generate a range but it's the simplest. If we use rand()%n we generate a number from 0 to n-1. By adding an offset to the result we can produce a range that is not zero based. The following code will produce 20 random numbers from 1 to 10:

A better method, though slightly more complicated, is given below. This overcomes problems that are sometimes experienced with some types of pseudo random number generator that might be supplied with your compiler. As before, this will output 20 random numbers from 1 to 10.