Tabris42
asked on
Unique, random 4 digit numbers
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)/size of(int);++ i)
{
randomnumbers[i] = (rand() % (high -low)) + low;
}
for(i = 0;i < sizeof(randomnumbers)/size of(int);++ i)
{
printf("%i, ", randomnumbers[i]);
}
printf("\n\n");
system("pause");
return 0;
}
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)/size
{
randomnumbers[i] = (rand() % (high -low)) + low;
}
for(i = 0;i < sizeof(randomnumbers)/size
{
printf("%i, ", randomnumbers[i]);
}
printf("\n\n");
system("pause");
return 0;
}
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_number s[j]) {flag = 1;break}}
}
}
}
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_number
}
}
}
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.
srand will not insure you get unique numbers. It just changes the seed value.
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.
Yours searches all previous numbers.
>>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.......
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.......
yep
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;