Q: Large array of random indexes
Author Message
Q: Large array of random indexes

I need to present an array of floating point numbers to a processing function
in a random order each time the function is called.  The array can be pretty
large (up to 20,000 elements).  The function in turn may be called tens of
thousands of times (maybe hundreds of thousands).

Currenty I use the following rand() and modulus routine to randomize
a set of indices from 0 to n.

void shuffle(int Maxneeded, int randindex[]){
int i,x;
for(i=0; i < Maxneeded; i++) randindex[i] = -1;
for(i=0; i < Maxneeded; i++){
x = rand() % Maxneeded;
while(randindex[x] !=-1){
x++;
x = x % Maxneeded;
}
randindex[x]=i;
}

Quote:
} /* end shuffle */

While it works, it is terribly slow - too slow.  On the other hand, qsort
when used to sort the same size random array is much, much faster.

Is there a way that I can use qsort to sort an array and have an index
follow the sorted number, similar to the following?

Given the following:
x = list of sequential numbers from 0 to n
y = n random numbers
x     y
------------
|  0  | rand |
|  1  | rand |
|  2  | rand |
|  3  | rand |
|  4  | rand |
|  5  | rand |
| ... | .... |
|  n  | rand |
------------

Sort based on y (taking the associated x with the sorted y) and then use x
as a source of randomized indexes.

Any help is apreciated.  I checked the faq and old news group postings
(all that had not expired), but could find no reference to this particular
problem.

Mon, 23 Dec 1996 04:36:41 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages