lundi 16 février 2015

Repeated seeded random number in c++

I am trying to get the random number (x) to get different result. Every time I run the program, I am given the same number, in result the same response. I am making a magic 8 ball. I am curious whether the srand or the rand is used wrong because I have tried messing around the



x= rand() % 16+1



Here is my code at the moment.



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

int main()
{
int x, y;
// do
{
cout << "Ask me a question. (Entering 20 will leave the program!)\n";
cin >> y;
if (y == 20 )
srand (time(NULL));
x = rand() % 16 + 1;
switch (x)
{
case 1:
cout << "Ask again\n";
break;
case 2:
cout << "Question too vague.\n";
break;
case 3:
cout << "Don't count on it.\n";
break;
case 4:
cout << "Yes.\n";
break;
case 5:
cout << "As I see it, yes.\n";
break;
case 6:
cout << "My reply is no.\n";
break;
case 7:
cout << "Cannot predict now.\n";
break;
case 8:
cout << "Outlook good.\n";
break;
case 9:
cout << "You may rely it.\n";
break;
case 10:
cout << "You will have to wait.\n";
break;
case 11:
cout << "Sure, why not?\n";
break;
case 12:
cout << "Go for it!\n";
break;
case 13:
cout << "Very doubtful.\n";
break;
case 14:
cout << "Concetrate and ask again.\n";
break;
case 15:
cout << "Who knows.\n";
break;
case 16:
cout << "The world may never know.\n";
break;
}
}
}

Aucun commentaire:

Enregistrer un commentaire