randomizing the answer to a Choice in C++

Hello there,

I am new to C++, and I am having some trouble with some code I am writing. I have written a program that works, but it is not quite what is wanted. Here is the problem:

Every time the door bell howls, provide the Count with a random trick or treat. 1 in 5 times the program should hand out candy to the little goblin, however the other 4 times, the goblin should be tricked.

My solution works but is not random. I am having trouble understanding how to adapt the srand or rand to make this work. I am only 8 weeks into this, and I am still learning the syntax. Here is the code I wrote. It executes fine, but doesn't fully fulfill the requirements of the problem. Can anyone help or provide insight as to how I should use rand or srand to do this?

#include <iostream>

using namespace std;
int main()
{

cout << "Happy Halloween! ";
cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";
cout << "\nPick a number between 1 and 5 to send a hobgoblin to ring his doorbell:\n ";

while (true) {

int Hobgoblin = 0;

cin >> Hobgoblin;
if (Hobgoblin == 3) {
cout << "Here is a big bag of candy, little Hobgoblin!\n ";
break;

} else if (Hobgoblin <= 2 ){
cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
cout << "Pick another Hobgoblin:\n ";
} else if (Hobgoblin >= 4 ){
cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
cout << "Pick another Hobgoblin:\n ";}

}

return 0;
}


sorry about the layout, I am having some trouble with the accepted "style".
Check out the example in the ref section of this site:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

And also see the see alsos!

Andy

P.S. And see "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
What you want to do is replace your user input with a call to rand(). You'll then process the returned value of rand() to yield a value between 1 and 5. This is normally done by taking the modulus 5 of the returned value, adding 1 to it, and assigning it to an int.

You use the srand() once, before your loop, to seed the random number generator. This prevents the RNG from always returning the same sequence of values to you.
Thank you Andy,

I had looked at the rand and srand links prior to posting, but was unsure that I fully understood them. I had never seen some of the tags that you had suggested looking at. Thank you for your help.

Christine
Okay. I have provided you with all the function for rand. That is one function. Now you just put somewhere that function for rand number. In the brackets of the function should be rand_number(5) - 1. So it can start from 1 to 5...

Now for example:

1
2
3
4
5
6
7
8
9
10
// Random


int i_need_number = 0;

i_need_number = rand_number(5) - 1;

cout << "WOW I DID GET RANDOM! " << i_need_number << endl;
......




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <ctime>

using namespace std;
int rand_number(int x);

int main()
{
    
    srand(time(NULL));

cout << "Happy Halloween! ";
cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";
cout << "\nPick a number between 1 and 5 to send a hobgoblin to ring his doorbell:\n ";

while (true) {

int Hobgoblin = 0;

cin >> Hobgoblin;
if (Hobgoblin == 3) {
cout << "Here is a big bag of candy, little Hobgoblin!\n ";
break;

} else if (Hobgoblin <= 2 ){
cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
cout << "Pick another Hobgoblin:\n ";
} else if (Hobgoblin >= 4 ){
cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
cout << "Pick another Hobgoblin:\n ";}

}
system("PAUSE");
return 0;
}

int rand_number(int x){
    return rand() % x;
}
Last edited on
mzimmers,

If I do that, it will pick one number between 1 and five to be random every time it runs? Am I understanding that correctly? I did read the Cplusplus notes on rand and srand, as well as surf the net.

Thank you for your help, I will try to do that. If I add the rand, will I need to remove the else if's to make it run correctly?

Again, thank you for your help!
Yes, it should return a number between 1 and 5. If you're not sure that you have your rand() set up properly, put it in a loop and print out, say, 100 instances of what it returns.

And, no, your while loop is essentially OK, except you haven't put a rand() in it (at least not one I can see).
Actually, yes, you do have some other issues. Here's one way it could work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <ctime>

using namespace std;
int rand_number(int x);

int main()
{
	int Hobgoblin;
	bool loopAgain = true;
	srand(time(NULL));

	cout << "Happy Halloween! " << endl;
	cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";

	while (loopAgain) {

		Hobgoblin = rand_number(5);

		if (Hobgoblin == 3) {
			cout << "Here is a big bag of candy, little Hobgoblin!\n ";
			cout << "Pick another Hobgoblin:\n ";
		} else if (Hobgoblin <= 2 ){
			cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
			loopAgain = false;
		} else if (Hobgoblin >= 4 ){
			cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
			cout << "Pick another Hobgoblin:\n ";}
	}
	system("PAUSE");
	return 0;
}

int rand_number(int x){
	return rand() % x;
}


But I'm not sure you want a loop here, or if you just want to run the rand() once, provide the trick or treat, and exit. You may want to modify it according to how you want it to behave.
If you're not sure that you have your rand() set up properly, put it in a loop and print out, say, 100 instances of what it returns.

You could even add checks to make sure the values are in range, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <ctime>
using namespace std;

// return random number between 0 and x-1
int rand_number(int x);

int main()
{
    srand(time(NULL));

    const int count = 10000;
    const int max_value = 5;

    int value_counts[max_value] = {0}; // make sure array is init to zero

    cout << "Looping " << count << " times..." << endl;

    for(int index = 0; count > index; ++index)
    {
        int value = rand_number(max_value);

        if(0 > value)
            cerr << "Underflow!" << endl;
        else if(max_value <= value)
            cerr << "Overflow!" << endl;
        else
            ++value_counts[value];
    }

    for(int index = 0; max_value > index; ++index)
    {
        cout << index << " = " << value_counts[index] << " ("
             << (100.0 * (double)value_counts[index])/(double)count << "%)" << endl;
    }

    system("PAUSE");

    return 0;
}

int rand_number(int x){
    return rand() % x;
}

Last edited on
Thank you happykiller, mzimmers, and andywestken! I will go work on this, and let you know how it turns out. Thank you!
Last edited on
Thanks for the help, I finished my code!
Topic archived. No new replies allowed.