advanced Random() functionality?

I have a need (or desire) to iteratively select a random value from an array, and each value is selected from the remaining values that have not been selected until they have all been selected. Does anyone have a suggestion for how this could be done?

Thanks.

Only one way you can do it - keep track of which ones you've already used with an array of flags. Get a random number. If the flag for that value shows it has been used, then get another random number, and keep going until you get one that hasn't been used. Then you need to figure out when you need to reset the flags, to flag them all as unused once again.

Regards,
Ray L.

Shuffle the array (or make a shuffled copy)...

perigalacticon:
I have a need (or desire) to iteratively select a random value from an array, and each value is selected from the remaining values that have not been selected until they have all been selected. Does anyone have a suggestion for how this could be done?
Thanks.

That’s easy: Let’s think of the array as a “deck of playing cards”,

Then "shuffle the cards " and draw them one by one, from first to last.

Here is a small example

byte numberArray[10];

  void shuffle(byte* arr, byte count)
{
  randomSeed(micros());
  int i, j;
  byte temp;
  for ( i =0;i<= count-1; i++) 
  {
    j=random(count) % count;
    temp= arr[ i ];
    arr[i]=arr[j];
    arr[j]=temp;
  }
}

void  printNumbers()
{
  for(int i=0;i<sizeof(numberArray);i++) Serial.print(numberArray[i]);
  Serial.println();
}


void setup() 
{
  Serial.begin(9600);
  for(int i=0;i<sizeof(numberArray);i++)numberArray[i]=i;
  printNumbers();
}

void loop() 
{
  if(Serial.read()=='s')
  {
    shuffle(numberArray,sizeof(numberArray));
    printNumbers();
  }
}

After sending letter ‘s’ from the serial monitor, numbers will be shuffled and printed.

Is that what you want?

Thanks these are all really good. I didn't consider that I need to do this only every 50ms or so, so I can take the time to do more processing such as tracking the array number selected.

There is no need to shuffle the array.

Fill an array with the N items you need to select from.
Set a 'length' variable to N.
loop:
  select a random number between 0 and 'length' -1'
  use that random number as an index into the array
  and select the item. Replace that item in the array by the last
  item in the array.
  decrement the 'length' variable by 1.
  if one needs another value goto loop.

Note that to select K values one only need K random numbers.

I think you want to say "swap that item in the array with the last item", not "replace it with the last item".

What you're describing is a shuffling algorithm. The only difference is that your implementation just performs one step of the shuffle each time you need a new value, and not all the steps at once at the beginning of the list.

Which I admit is a nice improvement.

Jiggy-Ninja:
I think you want to say "swap that item in the array with the last item", not "replace it with the last item".

In it's minimal form when one just needs K items extracted one at a time then one definitely replaces since the item being replaced has been used and is no longer needed. One can do a swap and then the last K items in the array are the K items one selected at random.

What you're describing is a shuffling algorithm. The only difference is that your implementation just performs one step of the shuffle each time you need a new value, and not all the steps at once at the beginning of the list.

It only becomes a shuffle if one swaps and applies the algorithm iterating 'length' from N down to 2.

I get it now. Because the last index is being "removed" from the array by the decremented length value, it doesn't matter what value is left in it.

For a single pass through the array that's fine, but it requires re-initialization before restarting the sequence. When using swap for a proper shuffle of the array, no re-initialization is needed, you can just reshuffle the array again as it is.