Yet another example, this one also produces an intarray populated with values 1-10, randomly ordered.
This example code should scale decently well (better than the previous example, at any rate) for larger number sets.
Once again, I'm sure others in the community could come up with some better solutions, so please add your answer if you have one.
//how big is our set of numbers?
int numset_count = 10;
//initialize an array of zeros
intarray numset = makearray(numset_count);
//populate numset with our set of numbers 1-10.
for (int i=1; i<=numset_count; i++) {
//randomEmptyIndex represents the first, fifth, eighth,
// etc, empty spot, as opposed to just the first, fifth,
// eighth, etc, index of the array. Also, for every value
// we enter into the array, there will be one less empty
// index to consider, so we can remove i from our random
// empty index count:
int randomEmptyIndex = duniform(1, numset_count+1-i);
//loop through numset, counting empty indeces until we
// get to the randomEmptyIndex empty index
int emptyIndexCounter = 0;
for (int j=1; j<=numset_count; j++) {
//if index j of numset is empty, increment our
// emptyIndexCounter counter variable
if (!numset) {
emptyIndexCounter++;
//if emptyIndexCounter has reached our
// randomEmptyIndex, we can set our i value there
// and break out of this inner for loop
if (emptyIndexCounter==randomEmptyIndex) {
numset = i;
break;
}
}
}
}
//numset is now populated with values 1-10, randomly ordered