#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
usingnamespace std;
vector <int> initDeck()
int randomize(int number0_1);
vector<int> shuffleDeck(vector<int> deckVector);
int main()
{
vector<int> deck = initDeck();
vector<int> newDeck = shuffleDeck(deck);
for(int i = 0; i <= 51; i++)
{
cout << newDeck[i] << " ";
}
system("PAUSE");
return 0;
}
vector <int> initDeck()
{
vector<int> deck(52);
for(int i=0; i <= 51; i++)
{
deck[i] = i;
}
return deck;
}
int randomize()
{
int seed = static_cast<int>(time(NULL));
srand(seed);
int card = static_cast<int>((rand()%52));
return card;
}
vector<int> shuffleDeck(vector<int> deckVector)//deckVector is the original deck in order
{
int temp, n, i, s;
n = 51;
for(n = 51; n >=0 ; n--)
{
// following code should swap out random values for the highest vector place
i = randomize();// randomize(0) generates random number between 0 and 51
temp = deckVector[n];
deckVector[n] = deckVector[i];
deckVector[i] = temp;
}
return deckVector;// this is supposed to be the shuffled deck
}
when I output the new vector, only produces this output(note- it is a random number every time, but it just repeats over and over):
I updated it for the full code, so that maybe someone can troubleshoot it. I fixed that section of the code now line 60: deckVector[n] = deckVector[i];, what could be wrong with my randomize function? It seems to give a different number everytime..
The problem is that you are seeding your RNG multiple times. As modern hardware is fast, no measurable time passes between function calls in shuffleDeck and you are essentually seeding RNG with the same value each call. Remember, you should seed only once. Ideally at the start of program.