Not random enough
Aug 9, 2012 at 6:06pm UTC
I have my battle program that i re wrote entirely and i cant seem to make the enemy choices random enough, it chooses the same thing like 4 or 5 times before going to a different one, how can i make it even more random?
I dont get any compiler errors or warings
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 98 99 100 101 102
#include <iostream>
#include <string>
#include <ctime>
#include <random>
using namespace std;
class Vars
{
public :
void Battle();
Vars()
{
PHealth = 100;
EHealth = 100;
}
private :
int PHealth; //Player Health
int EHealth; //Enemy Health
};
int main()
{
string choice;
Vars VO;
cout << "Welcome to the battle arena" << endl;
cout << "Please enter the number of the selection\n" << endl;
cout << "\n" ;
cout << "1. Begin" << endl;
cin >> choice;
if (choice == "1" )
{
VO.Battle();
}
}
void Vars::Battle()
{
int Pchoice; //Player choice
int Echoice; //Enemy Choice
bool end = false ;
cout << "Choose your attack\n" << endl;
cout << "1. Shotgun" << endl;
cout << "2. Machine Gun" << endl;
cout << "3. Knife" << endl;
cin >> Pchoice;
while (end != true )
{
time_t T;
time(&T);
srand(T);
int time;
time = rand() % 4;
switch (Pchoice)
{
case 0:
cout << "you used the Shotgun\n" << endl;
Echoice;
cin.get();
break ;
case 1:
cout << "You used the Machine Gun\n" << endl;
Echoice;
cin.get();
break ;
case 2:
cout << "You used the Knife\n" << endl;
Echoice;
cin.get();
break ;
}
switch (Echoice, time)
{
case 0:
cout << "Enemy used Punch\n" << endl;
Pchoice;
cin.get();
break ;
case 1:
cout << "Enemy used Kick\n" << endl;
Pchoice;
cin.get();
break ;
case 2:
cout << "Enemy used Slap\n" << endl;
Pchoice;
cin.get();
break ;
default :
cout << "Enemy attack missed\n" << endl;
}
}
}
Last edited on Aug 9, 2012 at 6:07pm UTC
Aug 9, 2012 at 6:17pm UTC
Your seeding the prng in the while loop, call srand only once.
Aug 9, 2012 at 6:25pm UTC
im not exactly sure how to do that. I put the random stuff outside the while loop and the enemy just gives the same attack.
Aug 9, 2012 at 7:42pm UTC
Move the srand(T); statement out of the loop. Otherwise you re-initializing it again to pretty much the previous value and it is the reason why the random doesn't appear to be as random as you are thinking it should be.
That should make things appear more random.
Abbott
Topic archived. No new replies allowed.