Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unique, random 4 digit numbers

Posted on 2003-10-27
8
Medium Priority
?
736 Views
Last Modified: 2010-04-15
I'm looking for a good method of creating 300 unique 4 digit random numbers. However, I'm unsure of how I can actually create these numbers (between 1000 and 9999), especially making them unique. Any help that can be offered is greatly appreciated.
0
Comment
Question by:Tabris42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 1

Accepted Solution

by:
guitardude101 earned 1000 total points
ID: 9631430
the secret to make sure they are unique is to use some type of hash table
in this example I allocate an array and use the subscript to see if I did that number yet. If I did not I mark the number taken by putting a 1 in it.

my random number functions parameters
randomnummber array to store the generated numbers
size is the number of elements
low is lowest number to generate
high is the highest number to generate

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv);
void createrandomnumbers(int randomnummber_array[], int size, int low, int high);

int main(int argc, char **argv)
{
      int randomnumbers[300];
      createrandomnumbers(randomnumbers, 300, 1000, 9999);

      return 0;
}



void createrandomnumbers(int randomnummber_array[], int size, int low, int high)
{
      int index;
      int randomnumber;
      int *hashtable;
      size_t bytestoallocate;

      bytestoallocate = ((high - low) + 1) * sizeof (int);
      hashtable = (int*) malloc(bytestoallocate);
      if (hashtable)
      {
            memset(hashtable, 0, bytestoallocate);
            
            for (index = 0; index < size; index++)
            {
                  while (1) {
                        randomnumber = (rand() % (high -low)) + low;
                        if (hashtable[randomnumber] == 0)
                        {
                              randomnummber_array[index] = randomnumber;
                              hashtable[randomnumber] = 1;
                              break;
                        }
                  }
            }
            free(hashtable);
      }
}
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9631446
I just noticed 1 thing
  the line
       if (hashtable[randomnumber] == 0)
  should be
        if (hashtable[randomnumber - low] == 0)


and
     the line
       hashtable[randomnumber] = 1;
    should be
       hashtable[randomnumber - low] = 1;
0
 
LVL 30

Expert Comment

by:Axter
ID: 9631528
Just use srand with a time function.
Example code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
{
      int low = 1000, high= 9999, i;
      int randomnumbers[3000];
      srand( (unsigned)time( NULL ) );

      for(i = 0;i < sizeof(randomnumbers)/sizeof(int);++i)
      {
            randomnumbers[i] = (rand() % (high -low)) + low;

      }

      for(i = 0;i < sizeof(randomnumbers)/sizeof(int);++i)
      {
            printf("%i, ", randomnumbers[i]);

      }

      printf("\n\n");
      
      system("pause");
      return 0;
}

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:Ajar
ID: 9631559
The following function tries to accomplish the  desired result in a intutive way.

void f()
{
     int r_numbers[300],i,j,flag;
     for(i=0;i <300;i++)
     {  
           flag = 1;
           while(flag)
           {
                 flag = 0;
                 r_numbers[i]= 1000 + (rand()%(9999-1000);
                 for(j = 0; j < i;j++) {if(r_numbers[i]==r_numbers[j]) {flag = 1;break}}
           }
     }
     
}
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9631597
That would work but mine is orderes of magnitude faster and why post after a correct answer
srand will not insure you get unique numbers. It just changes the seed value.
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9631609
The reason why mine if faster is I only search 1 element to see if the number was already done.
Yours searches all previous numbers.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9632757
>>srand will not insure you get unique numbers.

You're right.  I did not completely understand the question when I first read it.

However, your example should have a call to srand().
Otherwise, you'll get the same results everytime you run the program.

void createrandomnumbers(int randomnummber_array[], int size, int low, int high)
{
  srand( (unsigned)time( NULL ) );
// The rest of your code.......


0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9633029
yep
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question