All I need to know is how a good way to make sure integers between 1 and 52 wont be draw twice over in a for statement... I was thinking something like

srand (time(NULL));
int *drawn;            //The card to be drawn
int notdraw[52];    // All the cards that have already been drawn and should not be drawn again...
for (n=0;n<52;n++)
{
*drawn = new int;
drawn =  rand() % 52+1;
drawn = notdraw[n]
delete draw;
}

thats where I get jumbled up... Just been thinking of putting a for statement in that will process the drawn to look over every notdrawn and ensure its not using one of them... um did I just answer my own question... anyway plz help

Dani AI

Generated

Short summary and practical fix. The goal is to draw all 52 cards exactly once. The early code by used dynamic allocation per draw and comparisons inside a nested loop; that works but is error-prone, wasteful, and easy to get wrong (uninitialized array slots, off-by-one indexing, pointer misuse). was headed in the right direction by avoiding repeated linear searches, but std::random_shuffle is obsolete now — prefer a single shuffle of a deck container.

A robust, simple pattern: build a container that represents the 52 cards, shuffle it once with a modern RNG, then iterate the shuffled deck. This avoids duplicate-checking logic entirely and runs in O(n). Use std::shuffle with std::mt19937 seeded from std::random_device for good quality randomness and optional reproducibility (seed with a fixed value when you want repeatable runs).

Example (modern C++ style):

#include <vector>
#include <random>
#include <algorithm>
#include <iostream>
#include <string>

struct Card { int rank; int suit; };
// build deck, shuffle with mt19937, then print in order

Troubleshooting notes:

  • Do not allocate one int per draw with new/delete; use stack objects or containers (std::vector) instead.
  • Watch indexing: decide whether you use 0..51 or 1..52 and be consistent when mapping to rank/suit.
  • If you need reproducible shuffles (for tests), use a fixed seed for mt19937; otherwise seed from std::random_device.
  • Avoid rand()/srand() and rand() % n for production-quality randomness and to prevent modulo bias.

See std::shuffle for the recommended shuffle API and std::random_device / std::mt19937 for seeding and engine details: , std::random_device.

yeah sorry guys I had been thinkin about it for a while but posting this unclogged my mind, a little snippet of how I did it

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<string>
using namespace std;

int main(){
      srand (time(NULL));
      int *drawn;
      int nodraw[52];
      for (int n=0;n<52;n++)
      {
      drawn = new int;
      *drawn = rand() %52+1;
     
      cout << n+1 << ". " <<*drawn << endl;
       for(int x=n;x>0;x--)
      {
              if (*drawn == nodraw[x])
              {
                       cout << "Repeat" << endl;
                       }
      nodraw[n]=*drawn;
              }
      delete drawn;
      }
      system("PAUSE");
      }

sorry guys...

to just draw 52 cards in random order without duplicate draws, just fill an array/vector of size 52 with values [1,52] and do a std::random_shuffle on it.
to make 52 draws, marking/discarding duplicate draws, we can avoid the 52 linear searches:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
  std::srand( std::time(0) ) ;
  enum { NCARDS = 52 } ;
  bool already_drawn[NCARDS] = { false } ;
  for( int n=0 ; n<NCARDS ; ++n )
  {
    int drawn = rand()%NCARDS + 1 ;
    std::cout << n+1 << ". " << drawn << '\n' ;
    if( already_drawn[drawn-1] ) std::cout << "Repeat\n" ;
    else already_drawn[drawn-1] = true ;
  }
}
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.