1. ## A simple question about selecting elements in an array

How do you select MULTIPLE elements in an array randomly?

I'm trying to select 10 different elements in an array at once in a loop. The loop will be repeated over and over again, but the value of the 10 elements will be saved on to a global variable. Any suggestions, ideas are welcome. Help please....

Linus

ingravity@hotmail.com

2. //zMan -- Perhaps one way to do this will help you

int g_myArray[10] = {0};

void main(void)
{
int myArray[100] = {100,200,300,213,12,......,};

....

}

/*
The way this function works it grabs values from the entire
array and places them in the first ten entries. You can think of
it as a shuffle--except that the entire array is not out of order but
the first ten positions are.
It modifies the original array because it shuffles the values.
If you preferred not to modify the original array you can store the
values in another array of 10 elements. Checking for duplicates
would have to be accomplished by looping through the array of 10
prior to adding a new item to item to it.

*/
{
int nRandom, nTemp;

//Shuffle the first ten positions in the array
//We could shuffle the whole array but what is
// the purpose when we only need ten values?
for(int i = 0; i < 10; i++)
{
nRandom = rand() % nLastIndex;
nTemp = someArray[i];
someArray[i] = someArray[nRandom];
someArray[nRandom] = nTemp;
}

//do not..do not try to accomplish the assignment
//task in the loop above.... You may get duplicate values
for(i = 0; i < 10; i++)
{
g_myGlobalArray[i] = someArray[i];
}
}