First off, I have searched the forums and have not stumbled across the answer to my question. If I overlooked it feel free not to post here or close the thread or link me to the correct thread. I apologize if i missed it.

I'm trying to create a number gen for a...draw the name out of the hat kinda thing. So I want each number to be used only once. I've searched all over the net and the best solution I've found so far is to try and put each value into an array then have it check to see if the number is used yet and then not use it if found. But the source code wasn't listed for that he just kinda explained it and not being in C++ for too long I don't quite understand. If you know of a better way please explain it.

This is my code at the moment for the entire thing, there are commented out attempts within it:

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

using namespace std;

int quant;
char answer;
int change;
int output ;
char choice;
//int once[];

/*int checkNum()
{
    if (once[quant]) == 
}//checkNum*/





int randomize()
{
int x;
int y = 1;
int arr[quant];


 while (x != quant)
     {
         // while(output != arr[1 % y])// % arr[y])
         // {
          
          cout << (output = ((rand() % quant)+1)), "\n";
        // }
          output != arr[(y^0) % y];
        arr[y] = output;
         
         quant--;
          //x++;
          y++;
          

     }//while
     
     cin.get();
 
    
     }//randomize
       

int main()
{
    srand( time(NULL));//Different values for random.
    cout << "How many people to choose from?\n";
    cin >> quant;
    cout << quant << " people, this is correct? y/n\n";
    cin >> answer;
    cin.ignore();
    if ( answer == 'y' )
    {
         cout << "Ok, assign each person a number and I will randomize them.\n";
         cout << "Press ENTER to list numbers";
         cin.get();
         randomize();
         
while (choice != 'n')
{
    
    cout << "Would you like to try again? y/n \n";
     cin >> choice;
     if (choice == 'y')
     {
     main();
     }
     if (choice == 'n')
     {
     return 0;
     }
     else
     {
         cout << "Sorry, I didn't catch that.";
         }//else
         }//while
              
    }//if
    if ( answer == 'n' )
    {
         main();
    }//if
    if (answer != ('n')||('y'))
    {
        cout << "Error";
        cin.get();
        main();
        }//else
    
    return 0;
}//main

Recommended Answers

All 3 Replies

I would sugguest you use linked lists for this sort of thing (or a binary tree). But if that's outside your knowledge, you could use a horribly inefficient check loop:

int const MAX = 10;
int main()
{
int iNum[MAX], iRand(0);
bool bFlag(false);
for (int i(0); i < MAX; i++)
{
while (!bFlag)
{
bFlag = true;
iRand = rand() % MAX;
for (int ii(0); ii < i; ii++)
{
  if (iRand == iNum[ii])
  {
    bFlag = false;
    break;
  }
 }
}
cout << "\nNext unused number: " << iRand;
}
}

Although...with enough values to be searched for that method becomes exponentially more awful.

I wouldnt mind learning bout those things ill look them up, the loop is interesting but u said it was horribly inefficient and barely into this i dont want to already be doing things that dont work quite so well, I'm in the process of figuring out how to use sets.

I still end up looking up the lists and binary tree topics though as i'm eager to learn.

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.