Using elements in an array only once

Hi,

I'm using a one dimensional array with 10 elements whereby each element contains an actual pin output designator replicated twice. My function "beacon_select" chooses one of the elements at random from the array and displays it.

How do I make sure that the function does not duplicate (choose) the same element during it's random selection?

int ledBeacons [] = {3, 3, 4, 4, 5, 5, 6, 6, 7, 7};  //five led pin designators replicated twice in the array
.
.
.
void loop()
{
    int r = random (1,11);                        
    int beacon_select = ledBeacons [r];     //choose a random led from the array
    for (int i = r; i < 10; i++);                  //perform the random selection ten times and display this
    Serial.println (beacon_select);            
    digitalWrite (beacon_select, HIGH);
    delay (1000);
}
.
.
.

Any constructive ideas appreciated.

Why not have 5 elements in your array ?

Can you have a negative pin number?
Making a pin number negative might indicate that it has been used.

michinyon:
Why not have 5 elements in your array ?

The reason I have ten elements with replicated numbers is to save me looping through the array twice.
Here's a good analogy of what I'm trying to achieve:

A drawstring bag has 10 counters in it numbered 1 to 5, you choose a counter (randomly) one after the other. There is a chance that you could choose two of the same numbered counters consequetively but that is fine. You continue to draw counters from the bag until all ten are out. And that's it.

Easy :wink:
(for someone with experience that is)

What AWOL suggested, or create a second array, initialised to zeros. When you have selected an element, set it to a non zero value. Don't use any element with a non zero value.

I would keep a max value and swap with that.
In pseudo code:

max = 10;

loop:
while (max>0) {
int r = random (max);
selected = inputArray[r];
max = max - 1;
inputArray[r] = inputArray[max];
}

Basically you are drawing randomly from a set of 10, a set of 9, a set of 8, etc. until you draw "randomly" from a set of 1.

Forgive my sloppiness in the code and the probably obvious mistakes, but you get the picture.

http://c-faq.com/lib/shuffle.html

I like the shuffle routine that KeithRB points to. I think that you can continually reuse the array without re-initialization - it'll still have the same list of elements in random order, and, if you randomize it again, the order ought to still be random.

I'm pretty sure that this line    for (int i = r; i < 10; i++);doesn't really do what you wanted. If it does anything at all, it counts zero to nine quietly and quickly, and has no other effect. Did you really want that semicolon at the end of the line?

tmd3:
I like the shuffle routine that KeithRB points to. I think that you can continually reuse the array without re-initialization - it'll still have the same list of elements in random order, and, if you randomize it again, the order ought to still be random.

The OP mentioned using every value once.
If you still want to keep all original values for using the mechanism again, you can extend my proposal quite easily:

max = 10;

while (max>0) {
int r = random (max);
selected = inputArray[r];
max = max - 1;
inputArray[r] = inputArray[max];
inputArray[max] = selected;
}

That's just adding one line of code (the last one).
All values are still there, just in another order.
The OP probably doesn't care, as it needs to be randomized anyway :wink:
Again, just pseudo code, with probably stupid mistakes.