[NBLUG/talk] Request for help--how to program shuffling
Steve Zimmerman
stevetux at sonic.net
Tue May 20 05:26:01 PDT 2003
The following program will shuffle a vector of 52 cards--once. The
second, third, fourth, etc., times that I run the program, it prints
out exactly the same "random" shuffle as the first time.
Even if I recompile, it spits out the cards in the same order.
What I'm really after is a program that can take 52 things
and randomly shuffle them *every time I run it*. Any ideas?
Would yacc and lex maybe be a good idea?
FYI, I'm just doing this for fun, there's no duty involved or
commercial purpose.
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>
#include <iterator>
using namespace std;
int main() {
cout << "Illustrating the random_shuffle algorithm." << endl;
const int N = 52;
vector<int> vector1(N);
for (int i = 0; i < N; ++i)
vector1[i] = i + 1;
// Randomly shuffle the integers in vector1:
random_shuffle(vector1.begin(), vector1.end());
for (int i = 0; i < N; i++) {
if ((i % 4) == 0)
printf("\n");
switch(vector1[i]) {
case 1: printf("Ace of spades\t"); break;
case 2: printf("Ace of clubs\t"); break;
case 3: printf("Ace of hearts\t"); break;
case 4: printf("Ace of diamonds\t"); break;
case 5: printf("King of spades\t"); break;
case 6: printf("King of clubs\t"); break;
case 7: printf("King of hearts\t"); break;
case 8: printf("King of diamonds\t"); break;
case 9: printf("Queen of spades\t"); break;
case 10: printf("Queen of clubs\t"); break;
case 11: printf("Queen of hearts\t"); break;
case 12: printf("Queen of diamonds\t"); break;
case 13: printf("Jack of spades\t"); break;
case 14: printf("Jack of clubs\t"); break;
case 15: printf("Jack of hearts\t"); break;
case 16: printf("Jack of diamonds\t"); break;
case 17: printf("10 of spades\t"); break;
case 18: printf("10 of clubs\t"); break;
case 19: printf("10 of hearts\t"); break;
case 20: printf("10 of diamonds\t"); break;
case 21: printf("9 of spades\t"); break;
case 22: printf("9 of clubs\t"); break;
case 23: printf("9 of hearts\t"); break;
case 24: printf("9 of diamonds\t"); break;
case 25: printf("8 of spades\t"); break;
case 26: printf("8 of clubs\t"); break;
case 27: printf("8 of hearts\t"); break;
case 28: printf("8 of diamonds\t"); break;
case 29: printf("7 of spades\t"); break;
case 30: printf("7 of clubs\t"); break;
case 31: printf("7 of hearts\t"); break;
case 32: printf("7 of diamonds\t"); break;
case 33: printf("6 of spades\t"); break;
case 34: printf("6 of clubs\t"); break;
case 35: printf("6 of hearts\t"); break;
case 36: printf("6 of diamonds\t"); break;
case 37: printf("5 of spades\t"); break;
case 38: printf("5 of clubs\t"); break;
case 39: printf("5 of hearts\t"); break;
case 40: printf("5 of diamonds\t"); break;
case 41: printf("4 of spades\t"); break;
case 42: printf("4 of clubs\t"); break;
case 43: printf("4 of hearts\t"); break;
case 44: printf("4 of diamonds\t"); break;
case 45: printf("3 of spades\t"); break;
case 46: printf("3 of clubs\t"); break;
case 47: printf("3 of hearts\t"); break;
case 48: printf("3 of diamonds\t"); break;
case 49: printf("2 of spades\t"); break;
case 50: printf("2 of clubs\t"); break;
case 51: printf("2 of hearts\t"); break;
case 52: printf("2 of diamonds\t"); break;
}
}
return 0;
}
More information about the talk
mailing list