How to chose randomly from a array??

How do I set a int to a random array number? (look below)

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
  //Odd or Even!
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string qString;
    int qNum [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, };
    
    cout << "Ask me a yes or no question!" << '\n';
    cout << "Question: ";
    cin >> qString; 
    
    //This is where I need it to set a number to random!
    
    switch (qNum)
    {
        case qNum == 1:
        cout << "Yes." << '\n';
        break;
        
        case qNum == 2:
        cout << "No." << '\n';
        break;
        
        case qNum == 3:
        cout << "Maybe." << '\n';
        break;
        
        case qNum == 4:
        cout << "Out look good." << '\n';
        break;
        
        case qNum == 5:
        cout << "Out look not soo good." << '\n';
        break;
        
        case qNum == 6:
        cout << "Question too hazy." << '\n';
        break;
        
        case qNum == 7:
        cout << "Possible but not probable" << '\n';
        break;
        
        default:
        cout << "I have no idea!";
        return -1;
    }
    
    return 0;
}  
Following this
http://www.cplusplus.com/reference/cstdlib/srand/

I helped you use your case statement "randomly" and moved it to a function. I am not clear how you want to use an array. I think you should restate what you are trying to do.

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
//Odd or Even!
#include <iostream>
#include <string>

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

int aFunction(int qNum)
{


    switch (qNum)
    {
    case 1:
        cout << "Yes." << '\n';
        break;

    case 2:
        cout << "No." << '\n';
        break;

    case 3:
        cout << "Maybe." << '\n';
        break;

    case 4:
        cout << "Out look good." << '\n';
        break;

    case 5:
        cout << "Out look not soo good." << '\n';
        break;

    case 6:
        cout << "Question too hazy." << '\n';
        break;

    case 7:
        cout << "Possible but not probable" << '\n';
        break;

    default:
        cout << "I have no idea!";
        return -1;
    }

}


int main ()
{
    //This is where I need it to set a number to random!
    srand (time(NULL)); // seeding random number

    int timeToRunLoop = 100;

    for (int i = 0; i < timeToRunLoop; i ++)
    {
        aFunction(rand()%7);
    }

    return 0;
}


Ask me a yes or no question!
Question: r
No.
Question too hazy.
I have no idea!No.
I have no idea!Maybe.
Question too hazy.
I have no idea!Out look not soo good.
Out look good.
Yes.
Maybe.
I have no idea!I have no idea!No.

... 100 times... 
@FireCoder

I better way, would be to use an array with all the possible responses, and then cout a random one to answer the question. Like so..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	srand((unsigned)time(0)); 
	string qString;
	string qNum[] { "Yes.", "No.", "Maybe.", "Outlook good.", "Outlook, not so good.", "Question, too hazy.", "Possible, but not probable", "I have no idea!","Ask again, later" };
	int rand_answer;
	cout << "Ask me a yes or no question!" << '\n';
	cout << "Question: ";
	cin >> qString;

	rand_answer = rand() % 9;

	//This is where I need it to set a number to random!
	cout << qNum[rand_answer] << endl;
	
	return 0;
}
@FireCoder Thank you for your response.
I was lost...
cin >> qNum[rand() % 9];
@anup30

I think you meant cout << qNum[rand() % 9] << endl;

I put it with getting the random number first, then using that variable to cout the result, because I figured it would be easier for FireCoder to understand, at this point. But now, (s)he knows 2 ways it can be done.
Slight tweak(s) -- mainly to avoid counting answers!

Andy

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
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	srand((unsigned)time(0)); 
	const string qNum[] { "Yes.", "No.", // now wrapped to avoid ugly long line
                              "Maybe.", "Outlook good.", // and note C++11 style init.
                              "Outlook, not so good.", "Question, too hazy.",
                              "Possible, but not probable", "I have no idea!",
                              "Ask again, later" };
	const int answer_count = sizeof(qNum)/sizeof(qNum[0]); // calc num of elems
	cout << "Ask me a yes or no question!" << '\n';
	cout << "Question: ";
	string qString;
	cin >> qString;

	int rand_answer = rand() % answer_count; // better than magic number!

	//This is where I need it to set a number to random!
	cout << qNum[rand_answer] << endl;
	
	return 0;
}
Last edited on
Looking back, I sucked at c++ :P here is my new script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
    std::string answers[5]{"Yes.","No.","Maybe.","Probebly.","Probebly Not."};
    int foo;
    
    srand(time(NULL));
    
    std::cout << "Ask me a question:\t";
    std::cin.ignore();
    
    foo = (rand() % 5);
    
    std::cout << answers[foo];
    
    return 0;
}
closed account (E0p9LyTq)
@FireCoder

Why use the C pseudo-random generator? Maybe it is time to learn the C++ <random> library.

Yes, a bit more work to use, but the library gives a better distribution of random numbers.
@furryGuy I never knew there was one! I chick it out.
closed account (E0p9LyTq)
<random> is a C++11 addition.
Topic archived. No new replies allowed.