Random arrays compiler issue

The program I created is suppose to work as follows.

Program takes an array with predetermined values on it (in this case it takes the values of a deck of cards).

The program then randomly pulls a value from the array (to simulate someone pulling a card from a deck of cards).

The program then is suppose to take that value and put it into a different array as a single value.

And this is sort of what my program is doing. However, it only does this if I run it in the debugger. If I just let it run regularly the resulting array simply just repeats the first random value.

Why is it doing this?

Here is the code as it is currently.

[#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>

using namespace std;

void getNumofPlayers(int *players);
void getCard(int *card);
void randomGenerator(int *randomnum);

void main()
{
int card = 0;
int players = 0;
int counter = 1;
char hitorno = ' ';


getNumofPlayers(&players);

//make player array

int playerarray[5][8];

//Fill array with card values
for(int x = 0; x < 5; x++)
{
for(int y = 0; y < 8; y++)
{
getCard(&card);
playerarray[x][y] = card;
}
}

//Display the values for the dealer and the players based on the input

if (players >= 1 && players < 5)
{
cout << "Dealer: " << playerarray[0][0] << "\t" << playerarray[0][1] << "\t" << endl;

for(int a = 0; a < players; a++)
{
cout << "Player" << counter << ": " << playerarray[counter][0] << "\t" << playerarray[counter][1] << "\t" << endl;
counter++;
}
}

else
cout << "Invaild number of players";

for(int counter2 = 1; counter2 == players; counter2++)
{
int playersum = playerarray[counter2][0] + playerarray[counter2][1];

//Ask player if they want to hit
for (int counter3 = 2; counter3 < 8; counter3++)
{
//Only can hit if less than 21

if(playersum == 21)
{
cout << "Black Jack!!!" << endl;
break;
}
if(playersum > 21)
{
cout << "Bust!!!" << endl;
break;
}
else
if(playersum < 21)
{
cout << "Would you like to hit or fold? (H of F) ";
cin >> hitorno;

switch(hitorno)
{
case 'H':
{
cout << "Player" << counter2 << ": " << playerarray[counter2][counter3] << endl;

playersum += playerarray[counter2][counter3];

break;
}
case 'F':

counter3 = 8;

break;
default:
cout << "Invalid input" << endl;
}
}
}
}

system("pause");
}

void getNumofPlayers(int *players)
{
cout << "How many players? ";
cin >> *players;
}
void getCard(int *card)
{
int m = 0;
int n = 0;
int cardDeck[4][13] = {{2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}};

//Random number generator
srand(time(0));
m = rand() % 3;

srand(time(0));
n = rand() % 12;
// pick a card any card

*card = cardDeck[m][n];
}

void randomGenerator(int *randomnum)
{
//random number generator that gives the dealer a sense of unpredicablity.
int a = 0;
srand(time(0));
a = rand() % 5;
*randomnum = a;

}]
Put the code you need help with here.
[/code]
You should only call srand once in your program. At the beginning of main is a good place.

The reason for this is because srand is only used to initialize the random sequence you get from rand(). If you use the same seed you will get the same sequence of numbers from rand(). In your program you use srand(time(0)) before each call to rand(). time(0) returns the time in seconds so it is very likely that you will get the same number each time, which means you also will get the same number from rand().

I see you have failed to use the [code] tag. You are supposed to place your code between [code] and [/code]. Not between [ and ].
Last edited on
Topic archived. No new replies allowed.