| | |
Question about Random Number Generators
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
I have a question regarding random number generators. I have already looked at the thread teaching me how to generate random numbers. My question is, is there a way for the generator to only generate one number 4 times? i am trying to randomly generate a deck of cards where the generated numbers are 1 - 12. Since in a real deck there is only four of each cards, I am wondering if this is possible.
Thanks in advance
Austin
P.S. please don't scold me for posting the same thing again since all the other threads are old, i did not want to ressurect old threads.
Thanks in advance
Austin
P.S. please don't scold me for posting the same thing again since all the other threads are old, i did not want to ressurect old threads.
Create a class called 'deck'
Initialise it with 52 cards.
Implement a function called 'shuffle', which randomises the order of 52 cards.
Implement a function called 'deal', which gives the next card in the pack. Calling deal 52 times will give you the whole pack, in a random order.
You don't need a random number generator with a series of magic properties.
Initialise it with 52 cards.
Implement a function called 'shuffle', which randomises the order of 52 cards.
Implement a function called 'deal', which gives the next card in the pack. Calling deal 52 times will give you the whole pack, in a random order.
You don't need a random number generator with a series of magic properties.
>>i did not want to ressurect old threads.
Good -- you might have teen tounglashed had you done that
Write a function that keeps track of all the random numbers it creates. Put the numbers into an array then each time a number is generated search the array to see how many times that number has been generated. You want the numbers 1-12 then create an int array of 12 elements and initialize them to zero. If you generate the number 2 then you will increment the 2nd element of the array -- array[1] is the 2nd array element.
Good -- you might have teen tounglashed had you done that

Write a function that keeps track of all the random numbers it creates. Put the numbers into an array then each time a number is generated search the array to see how many times that number has been generated. You want the numbers 1-12 then create an int array of 12 elements and initialize them to zero. If you generate the number 2 then you will increment the 2nd element of the array -- array[1] is the 2nd array element.
C++ Syntax (Toggle Plain Text)
int array[12] = {0}; .. int gennum() { while(1) { int n = rand() % 12 + 1; if( array[n-1] < 4) { array[n-1]++; return n; } }
Last edited by Ancient Dragon; Mar 26th, 2008 at 5:56 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
Create a class called 'deck'
Initialise it with 52 cards.
Implement a function called 'shuffle', which randomises the order of 52 cards.
Implement a function called 'deal', which gives the next card in the pack. Calling deal 52 times will give you the whole pack, in a random order.
Last edited by ElectorCount; Mar 26th, 2008 at 6:46 pm.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
Here's the basic class. You'll have to fill in the shuffle routine. As an aside, normal decks have 52 cards, which would be the numbers 1-13, not 1-12.
To use it in the program:
C++ Syntax (Toggle Plain Text)
class Deck{ public: list<int> cards; // cards in the deck void init(){ // puts all 52 cards in deck cards.clear(); // start with an empty deck for(int i = 0; i < 52; i++){ // fill it with 52 cards cards.push_back((i/4)+1); // numbers 1-13 only } } void shuffle(){ // .. shuffle deck randomly here } int getTopCard(){// deal a card - i.e. get top card, remove it from the deck int ret = 0; if(cards.size() > 0){ ret = cards.front(); cards.erase(cards.begin(); } return ret; // return 0 if no more cards, or the top card } Deck(){ // constructor init(); // fill the deck shuffle(); // shuffle it } };
To use it in the program:
C++ Syntax (Toggle Plain Text)
int main(){ Deck myDeck; // create a shuffled deck cout << "Top card is " << myDeck.getTopCard() << endl; }
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
thanks for the help
my shuffling code is this:
is there any way to use this in the code you gave me?
my shuffling code is this:
C++ Syntax (Toggle Plain Text)
{ int deck[52]; // array of cards; srand(time(0)); do { // Shuffles cards for (int index=0; index<(52-1); index++) { int r = index + (rand() % (52-index)); int temp = deck[index]; deck[index] = deck[r]; deck[r] = temp; } } }
is there any way to use this in the code you gave me?
•
•
Join Date: Mar 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
Yeah. Put it in the shuffle member function of the Deck class.
Because you're using the [] (which can't be used on lists), change thelist<int> cards;tovector<int> cards;, andret = cards.front(); cards.erase(cards.begin();toret = cards.back(); cards.pop_back();.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
i tried that and im confused as what to do with the array i created. Also, im using Microsoft visual C++ 2008 and it doesnt seem to like your code. Is there any specific thing i need to include?
C++ Syntax (Toggle Plain Text)
#include <vector> using namespace std;
If you have, can you post the revised code and be more specific on the error(s)?
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
The vector holds all the cards in the deck. You can shuffle its contents, and pick out a card (easiest to do this from the back of the vector - i.e. the last element in it).
You'll likely need to include:
at the top of your program.
As you haven't included your code or the errors you got, I can't help further.
You'll likely need to include:
C++ Syntax (Toggle Plain Text)
#include <vector> using namespace std;
As you haven't included your code or the errors you got, I can't help further.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: winsock2 linker error
- Next Thread: Problme overloading operators
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






