Question about Random Number Generators

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 9
Reputation: ElectorCount is an unknown quantity at this point 
Solved Threads: 0
ElectorCount ElectorCount is offline Offline
Newbie Poster

Question about Random Number Generators

 
0
  #1
Mar 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Question about Random Number Generators

 
0
  #2
Mar 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Question about Random Number Generators

 
0
  #3
Mar 26th, 2008
>>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.
  1. int array[12] = {0};
  2. ..
  3. int gennum()
  4. {
  5. while(1)
  6. {
  7. int n = rand() % 12 + 1;
  8. if( array[n-1] < 4)
  9. {
  10. array[n-1]++;
  11. return n;
  12. }
  13.  
  14. }
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: ElectorCount is an unknown quantity at this point 
Solved Threads: 0
ElectorCount ElectorCount is offline Offline
Newbie Poster

Re: Question about Random Number Generators

 
0
  #4
Mar 26th, 2008
Originally Posted by Salem View Post
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.
any way you could help me do that? i am kind of a novice.
Last edited by ElectorCount; Mar 26th, 2008 at 6:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Question about Random Number Generators

 
0
  #5
Mar 26th, 2008
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.

  1. class Deck{
  2. public:
  3. list<int> cards; // cards in the deck
  4.  
  5. void init(){ // puts all 52 cards in deck
  6. cards.clear(); // start with an empty deck
  7.  
  8. for(int i = 0; i < 52; i++){ // fill it with 52 cards
  9. cards.push_back((i/4)+1); // numbers 1-13 only
  10. }
  11. }
  12.  
  13. void shuffle(){
  14. // .. shuffle deck randomly here
  15. }
  16.  
  17. int getTopCard(){// deal a card - i.e. get top card, remove it from the deck
  18. int ret = 0;
  19.  
  20. if(cards.size() > 0){
  21. ret = cards.front();
  22. cards.erase(cards.begin();
  23. }
  24.  
  25. return ret; // return 0 if no more cards, or the top card
  26. }
  27.  
  28. Deck(){ // constructor
  29. init(); // fill the deck
  30. shuffle(); // shuffle it
  31. }
  32. };

To use it in the program:
  1. int main(){
  2. Deck myDeck; // create a shuffled deck
  3.  
  4. cout << "Top card is " << myDeck.getTopCard() << endl;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: ElectorCount is an unknown quantity at this point 
Solved Threads: 0
ElectorCount ElectorCount is offline Offline
Newbie Poster

Re: Question about Random Number Generators

 
0
  #6
Mar 26th, 2008
thanks for the help

my shuffling code is this:

  1. {
  2. int deck[52]; // array of cards;
  3. srand(time(0));
  4.  
  5. do
  6. {
  7. // Shuffles cards
  8. for (int index=0; index<(52-1); index++)
  9. {
  10. int r = index + (rand() % (52-index));
  11. int temp = deck[index]; deck[index] = deck[r]; deck[r] = temp;
  12. }
  13. }
  14. }

is there any way to use this in the code you gave me?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Question about Random Number Generators

 
0
  #7
Mar 26th, 2008
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 the list<int> cards; to vector<int> cards; , and ret = cards.front(); cards.erase(cards.begin(); to ret = cards.back(); cards.pop_back();.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: ElectorCount is an unknown quantity at this point 
Solved Threads: 0
ElectorCount ElectorCount is offline Offline
Newbie Poster

Re: Question about Random Number Generators

 
0
  #8
Mar 26th, 2008
Originally Posted by dougy83 View Post
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 the list<int> cards; to vector<int> cards; , and ret = cards.front(); cards.erase(cards.begin(); to ret = cards.back(); cards.pop_back();.
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Question about Random Number Generators

 
0
  #9
Mar 27th, 2008
Originally Posted by ElectorCount View Post
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?
Did you put these lines at the top?
  1. #include <vector>
  2. using namespace std;
http://www.cplusplus.com/reference/stl/vector/
If you have, can you post the revised code and be more specific on the error(s)?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Question about Random Number Generators

 
0
  #10
Mar 27th, 2008
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:
  1. #include <vector>
  2. using namespace std;
at the top of your program.

As you haven't included your code or the errors you got, I can't help further.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC