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.

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.

>>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.

int array[12] = {0};
..
int gennum()
{
   while(1)
   {
        int n = rand() % 12 + 1;
        if( array[n-1] < 4)
        {
            array[n-1]++;
            return n;
        }
  
}

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.

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.

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:

int main(){
   Deck myDeck;   // create a shuffled deck

   cout << "Top card is " << myDeck.getTopCard() << endl;
   }

thanks for the help

my shuffling code is this:

{
    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?

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(); .

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?

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?

#include <vector>
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)?

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:

#include <vector>
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.

ok I'm a slow typer...

commented: Happens to me all the time! +2

Here are the many error messages i got. The code is around 300 some lines so if you want to see it i can post it but just ask.

1>------ Build started: Project: FinalProject, Configuration: Debug Win32 ------
1>Compiling...
1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>Source.cpp
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(29) : error C2664: 'carddeck' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(30) : error C2664: 'comp' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(57) : error C2664: 'cards' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(63) : error C2664: 'cards' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(69) : error C2064: term does not evaluate to a function taking 6 arguments
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(94) : error C2664: 'cards' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(103) : error C2664: 'cards' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(269) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(290) : error C2059: syntax error : '}'
1>Build log was saved at "file://c:\Documents and Settings\Austin\Desktop\Austin\FinalProject\Debug\BuildLog.htm"
1>FinalProject - 8 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The problem's in your code; without seeing your code it's a bit hard to see where the problem is...

This is all that's required in the shuffle member function:

void shuffle(){
// 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;
            }
        }

The 'do' block must be closed with a 'while();'. The 'int deck[52]' isn't required in the shuffle function, as all the cards in the deck are stored in the class already in the vector.

It's best if you attach your code, otherwise it's harder to help. There's an attach icon (paperclip) available when you reply to thread.

here is my code.

SOurce.cpp

deck.h

//Source.cpp
//Austin
//Mar. 25, 2008
//Im not a very experianced programmer so i have been trying to run the program with functions.  
//the finished product will need a class.

#include <iostream>
#include <iomanip>
#include <time.h>
#include <vector>
#include "deck.h"
using namespace std;

void intro();
void carddeck(int deck[53]);
void cards(int deck[53], int howmany, int count, int player, int total1, int total2);
void comp(int deck[53], int howmany, int count, int player, int total1, int total2);
void player(int deck[53], int howmany, int count, int player, int total1, int total2);
void winlose(int total1, int total2);

int main()
{
	int deck[53];
	int howmany = 0;
	int count = 1;   //keeps track of the cards that have been removed from deck
	int total1 = 0;
	int total2 = 0;
	int player = 0;
	intro();
	carddeck(deck[53]);
	comp(deck[53], howmany, count, player, total1, total2);
	
	
	return 0;
}

Lines 30 and 31. Arrays are not passed that way. Your functions want the entire array. deck[53] is a single integers and it's also a segmentation fault since deck's indexes only exist from 0 to 52. Instead of this:

carddeck(deck[53]);

the call should be this:

codedeck (deck);

Make that change every time you are trying to pass an entire array to a function. Leave the brackets off. The brackets go with the function headers, not the function calls.

The problem's in your code; without seeing your code it's a bit hard to see where the problem is...

No it's not. Almost every error is ... error C2664: 'carddeck' : cannot convert parameter 1 from 'int' to 'int []' You are passing an integer in parameter 1 of carddeck and the function wants an integer array. Read the errors and look at the lines specified. Then look at the variable types and find the mismatch.

Thank you VernonDozier, now I only have 2 errors and 2 warnings.

1>------ Build started: Project: FinalProject, Configuration: Debug Win32 ------
1>Compiling...
1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>SOurce.cpp
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(71) : error C2064: term does not evaluate to a function taking 6 arguments
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(271) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(292) : error C2059: syntax error : '}'
1>Build log was saved at "file://c:\Documents and Settings\Austin\Desktop\Austin\FinalProject\Debug\BuildLog.htm"
1>FinalProject - 2 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thank you VernonDozier, now I only have 2 errors and 2 warnings.

1>------ Build started: Project: FinalProject, Configuration: Debug Win32 ------
1>Compiling...
1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>SOurce.cpp
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(71) : error C2064: term does not evaluate to a function taking 6 arguments
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(271) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\documents and settings\austin\desktop\austin\finalproject\source.cpp(292) : error C2059: syntax error : '}'
1>Build log was saved at "file://c:\Documents and Settings\Austin\Desktop\Austin\FinalProject\Debug\BuildLog.htm"
1>FinalProject - 2 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Ignore the warnings for now, focus on the errors. The second one (line 292) is due to this:

do 
   {    
        // Shuffles cards
        for (int index = 1; index < (53-1); index++) 
	{
            int r = index + (rand() % (53-index));
            int temp = deck[index]; deck[index] = deck[r]; deck[r] = temp;
        }
		//converts cards values into 1-13
	for (int index = 1; index < 53; index++)
	{
		deck[index] = (deck[index]/4)+1;
	}
    }

You have a "do" without a "while" at the end. For the first one, repost your new function call and, if you have changed the function header at all (which I don't think you needed to do), repost it too.

I just removed that do loop and the error is gone.

source.cpp(71) : error C2064: term does not evaluate to a function taking 6 arguments

SOurce.cpp

I just removed that do loop and the error is gone.

source.cpp(71) : error C2064: term does not evaluate to a function taking 6 arguments

[ATTACH]5596[/ATTACH]

void comp(int deck[53], int howmany, int count, int player, int total1, int total2)
{
cout<<"The computer will start: "<<endl;
char answ;
player = 1;
howmany = 2;
cards(deck, howmany, count, player, total1, total2);
do
{
if (total1 <= 16)
{
howmany = 1;
cards(deck, howmany, count, player, total1, total2);
}
else
{
if (total1 <=21)
{
player(deck, howmany, count, player, total1, total2);
}
else
{
cout<<"The computer has BUSTED "<<endl;
cout<<"YOU WIN!!!!!!! "<<endl<<endl;
cout<<"Would you like to play again? (y/n) ";
cin>>answ;
if (answ == 'y')
{
int main();
}
}
}
}
while((total1 <= 16)&&(total1 <= 21));
}

You have an integer called "player" (lines 1, 5, 7, 13, 19 (in parameter list)) and a function called "player" (line 19). The compiler is getting confused about which "player" you are referring to. Change the integer player to something else in the lines above (1, 5, 7, 13, 19) and keep the function called "player" the same. To avoid confusion, at the top of your file change this line to whatever you changed that integer to in the function:

void player(int deck[53], int howmany, int count, int player, int total1, int total2);

The compiler will let you get away with it, but changing the name will avoid confusion. You should probably change any integer in "main" called "player" to something else too to avoid confusion.

Thanks you guys for all your help!

My last question is how would i use the class in Microsoft visual C++. It is giving me some errors because it doesn't recognize vectors.

Thanks you guys for all your help!

My last question is how would i use the class in Microsoft visual C++. It is giving me some errors because it doesn't recognize vectors.

That seems very odd to me. I have to believe that Visual C++ allows you to use vectors. Post the code and the exact error please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.