How to add in a random seed. with desired input

Pages: 12
hi i am doing a program that requires me to input a random seed (already chosen) and input a maximum of 5 friends to text, randomly choose one and output the chosen name and the names that did not get chosen. i am trying to hard code the sites preset input, but i dont know how to add in the seed generator, so the program could work without hard coding it. please help me!

the first set of inputs is:
21(seed)
adam
bill
cat
damion
edward.

//and cat would be the chosen friend.


first one is my attempt to hard code the site, second is without the seed generator.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] = arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[6];
    int num_of_names = 5;
    int seed;
    int N = 6;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> seed;

    if (seed == 21)
    {

    for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> num_names[counter];


    }
    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

        cout << "These other friends didn't make the cut:" << endl;

            for (int i = 0; i < num_of_names; ++i)
            {
                cout << num_names[i] << endl;
            }

    return 0;
    }

    else if (seed == 42)
    {
        for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> num_names[counter];


    }
    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

        cout << "These other friends didn't make the cut:" << endl;

            for (int i = 0; i < num_of_names; ++i)
            {
                cout << num_names[i] << endl;
            }

    return 0;

    }

    else if (seed == 101)
    {
        for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> num_names[counter];


    }
    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

        cout << "These other friends didn't make the cut:" << endl;

            for (int i = 0; i < num_of_names; ++i)
            {
                cout << num_names[i] << endl;
            }

    return 0;

    }
    return 0;

}


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
45
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] = arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[6];
    int num_of_names;
    int N = 6;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> num_of_names;

    for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> num_names[counter];
    }

    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

    cout << "These other friends didn't make the cut:" << endl;

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
Last edited on
Wait, I don't understand. You tell the user "Enter seed" but then store the input in a variable that limits the number of friends that can be entered. Is the for condition wrong, or are you missing an input variable?
closed account (j3Rz8vqX)
What is the significance of 21, 42, and 101?

Edit:
Generally a seed is implanted into the srand function to control randomness.

srand(seed);

High possibility I am misinterpreting something here.
Last edited on
this is the question: Write a program that randomly determines whom the user should send a text message to. Allow the user to enter five friends that will be used for the selection.. . .

this is the givin input that my program will be tested with: Program Input
Standard Input
21
Adam
Bill
Cat
Damion
Edward
Program Output
== Who Should I Text? ==
Enter seed
Enter friend 0
Enter friend 1
Enter friend 2
Enter friend 3
Enter friend 4
You should text: Cat
These other friends didn't make the cut:
Adam
Bill
Damion
Edward
closed account (j3Rz8vqX)
My assumption is that you would like to manually control the seed of rand().
So when you prompt for the seed and a value of 21 is entered, it would produce Cat as the chosen seed.

Based on the shorten code:

Declare an integer named seed.
Move srand below your prompt for the seed. And modify it to be:
srand(seed);

Then clean up your remaining code; you should be okay.

Should work.
ok this is what i have come up with. this code allows me to input friends but it gets a runtime error after the 5th name inputted. what can i do so it can work properly?

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
45
46
47
48
49
50
51

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] = arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[5];
    int num_of_names = 0;
    int seed;
    int N = 5;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> seed;

	srand(seed);

    while (std::cout << "Enter friend " << num_of_names << endl,
            std::cin  >> num_names[num_of_names])
          {
                ++num_of_names;
          }


    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

    cout << "These other friends didn't make the cut:" << endl;

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
closed account (j3Rz8vqX)
Eh???!!

Of course it would, haha.

Okay, sorry about that.

Your while loop is incorrectly used/implemented.

Stick with your for-loop and set num_of_names equal to 5(where you declared/initialized it).

Edit:
Also remove your variable N, it isn't being used and
srand(time(NULL));
since you're declaring it below.

Edit #2:
You don't need
std::
in front of
cout
since you've included
using namespace std;
.

If you decide you really want to use a while loop, below has the design/examples:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
ok it worked, but it scrambles up the last two names, or all the names, in the outputted names. why would that happened, only 1 of the 3 test cases is correct...
this is the new code.

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
45
46
47
48
49
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] = arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[5];
    int num_of_names = 5;
    int seed;
    int N = 5;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> seed;

	srand(seed);

    for(int i = 0; i < num_of_names; i++)
    {
        cout << "Enter friend " << i << endl;
        cin >> num_names[i];

    }

    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

    cout << "These other friends didn't make the cut:" << endl;

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
closed account (j3Rz8vqX)
Since you've already done a lot, maybe something like this:
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
45
46
47
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] = arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    //srand(time(NULL));//Commented out for other intended uses below
    string num_names[6];
    int num_of_names=5;//Modified num_of_names to be 5
    int seed = 0;//Added seed integer
    //int N = 6;//Commented out useless/unused variable

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> seed;//Modified data entry to reflect actual seed

    for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> num_names[counter];
    }

    srand(seed);//call the srand function with seed instead of time, and after seed was declared.
    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

    cout << "These other friends didn't make the cut:" << endl;

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}

Try it.

Have fun.

Also what are you other test cases.

Given: 21 = Cat, ? = ?. ? = ?.
the same thing happens with the above code, and the other two test cases are, 42 = Dave, 101 = Lay
oh and the only one that works is 101 = Lay.
closed account (j3Rz8vqX)
My psychic powers are not enabling me to project the test data contents to my device.

Sorry insufficient data. =D
test case 1:

21
Adam
Bill
Cat
Damion
Edward

test case 2:

42
Dave
Bernard
Fred
Edna
Sid

test case 3:

101
Fastow
Skilling
McMahon
Lay
Watkins
closed account (j3Rz8vqX)
As you've stated, I got 42 = Dave, and 101 = Lay.

What were my goals?

Who was I suppose to receive from those values?
I tried your program with several test cases and it seemed to work for me.

Not sure what you meant by "42 = Dave, 101 = Lay"


i mean the list of names that did not get picked.

the first test case should output:
adam
bill
damion
edward

second:
Bernard
fred
edna
sid

the last one is correct.
I don't know that I can duplicate your results. Our random number generators are likely different unless we're using the same run-time library.
hmm ok, i guess this will go finished/ but unfinished. :/ good thing i ahve a good grade in the class.
closed account (j3Rz8vqX)
If the names that did not get pick are incorrect, then maybe we're approaching the rebuilding of the non selected names incorrectly.

Possibly something in the function (get_random_name).

arr[num] = arr[nums - 1];

The above code swaps:
-The person at the index of num(our random) with the person at the index of nums-1(your maximum number of people minus one: the last person)

Maybe instead of swapping the missing with the last, we should implement a design to shuffle all of the remaining folks down a notch?

Do you need help with that too? or do you've got it?
Pages: 12