Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 12th, 2003, 04:50 PM
 kavi_s
Junior Member

Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
rand() to generate ARRAY of numbers

Hi all,

I need to generate an array of random integers. The number of integers can vary depending on some formula. The most important thing is that the integers MUST be unique. I can't have duplicate numbers occurring throughout the array. How can I go about this? At the moment, I just have a for loop that iterates as many times are there is space in the array, and I call the "rand" function each time, like:

Code:
```  array[i] = (int)( ( ( (double)rand() )/RAND_MAX)*10);
//I want the range to be from 0 to 10
//i is the for loop variable```

Obviously, this does not take care of UNIQUENESS.
Is there any function that can generate an ARRAY instead which contains UNIQUE integers???

THanks!
Kavi

#2
June 12th, 2003, 05:13 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
one way would be to use two arrays. you say the random number is going to be between 1 and 10? so that's a maximum of 9 possible unique number then.

build two arrays, one with the number of elements in your required range which is 9 in your case i think. fill the array using a for loop in a sequential non-random way with the actual numbers - from 1 to 9.

make another array with the same number of elements, 9. do another for loop but this time fill each element with an entierely random value - don't even bother restricting or confining it to a particular range, so however the random numbers come - doesn't matter.

then using some sort of sort, like qsort, sort the random array numerically, and in the same way, at the same time in parallel sort the other array. the other array will end up with all your numbers once and only once, in a random order.

#3
June 12th, 2003, 05:28 PM
 kavi_s
Junior Member

Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
You are a genius! Thanks for your wonderful idea! I just tested it, and it's totally cool

Thanks!!

#4
June 12th, 2003, 07:25 PM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
If you want the range to be from 0..10, then that is 11 unique numbers, not 9.

I think a better way of solving this is like so (if, in fact, you plan on having exactly the same number of elements in the array as the maximum possible unique numbers, which seems to be the case):

Use only one array, of 11 elements. Fill the elements incrementally 0, 1, 2, ... 10 so that the index = the value within the index.

Now, randomly sort the array. How do you do this to make sure every index gets sorted? Swap elements exactly 11 times - the first index is the loop counter (0..10, incrementally), and the second index is random.

Here's the source, in case you didn't follow:

Code:
```#include <stdio.h> // printf()
#include <stdlib.h> // rand()
#include <time.h> // time()

int main()
{
// declaration
int array[11]; // elements 0..10
int i;

// step 1: initialize the array
for (i=0; i<=10; i++)
array[i] = i;

// step 2: sort the array
srand( (unsigned)time( NULL ) ); // randomize the rand() function
for (i=0; i<=10; i++)
{
int index1 = i;
int index2 = rand()%11; // x % 11 = 0..10

// swap elements index1 and index2
int temp;
temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}

// show the results.
for (i=0; i<=10; i++)
printf("array[%d] = %d\n",i,array[i]);

return(0);
}```

#5
June 12th, 2003, 08:58 PM
 kavi_s
Junior Member

Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
Hey Jason

That helped a lot too....Thank you very much!! Appreciate it

Kavi

#6
June 13th, 2003, 02:11 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
! i have no idea why i got 9! i was reading about sets and unions etc and wasn't understanding what i was reading just previous to seeing this question, so i blame my confusion on that but then it is actually 10 not 11! (1..10) doesn't matter though.

that is definetely a better way as it doesn't use a 2nd array, so i guess it must be more efficient.

and the array that you fill sequentially then sort randomly - that could contain anything - pointers, characters, other ints, whatever. not necessarily values that reflect the index.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > rand() to generate ARRAY of numbers