Hello!
I'd like to know how to perform a simple task: scrambling the order of the contents of an array. For example, I have an array like this: char players[] = {'D', 'A', 'E', 'S', 'M'}; I want them scrambled in a random order (like shuffling cards). What is the quickest and easiest way to do that? Are there any functions available?
Thanks!
Okay, so how would I "scratch out" an item in an array?
Visit every element of the array and swap the value there with any other random element of the array.
Oh, and Happy Anniversary!
Okay, so how would I "scratch out" an item in an array?
Set it to zero, or -1 or some other (for your application) impossible value.
Why would you bother scrambling the elements in an array?
Just create another array with the same number of elements - each element is an index to one element in the first array. Just populate the second array in a random order.
...R
Perhaps he'm RAM constrained and would rather use the memory he has for some equally as important!
There doesn't seem to be any point inventing the OPs answers. Patience...
...R
lloyddean:
Perhaps he'm RAM constrained and would rather use the memory he has for some equally as important!
He, that was my point - we just don't know yet.
Robin2:
Why would you bother scrambling the elements in an array?Just create another array with the same number of elements - each element is an index to one element in the first array. Just populate the second array in a random order.
The problem with creating a new array populated with the items in array 1 in random order using only random() is that it might pick the same item twice. The random function can't be programmed to chose (for example) a number from 1 to 10, but skip 2 and 7. I could just repeat random() until a new number is chosen, but that is rather inefficient.
AWOL:
Set it to zero, or -1 or some other (for your application) impossible value.
That means I will have to do a while loop to random until I come across a value that isn't 'Z' or some other placeholder.
I have attempted lloyddean suggestion (randomly swapping items of the array with each other):
#include <TrueRandom.h>
char players[] = {'D', 'A', 'E', 'S', 'M'};
char placeholder1;
char placeholder2;
byte randomPH1;
byte randomPH2;
void setup(){
Serial.begin(9600);
for(byte i = 1; i <= TrueRandom.random(5, 21); i++){ //switch players 5 to 20 times
randomPH1 = TrueRandom.random(0, 5); //random player 1
randomPH2 = TrueRandom.random(0, 5); //random player 2
placeholder1 = players[randomPH1]; //non changing placeholder for random player 1's name
placeholder2 = players[randomPH2]; //non changing placeholder for random player 2's name
players[randomPH1] = placeholder2; //change random player 1 to random player 2
players[randomPH2] = placeholder1; //change random player 2 to random player 1
}
Serial.print("Team 1: ");
Serial.print(players[0]);
Serial.print(", ");
Serial.print(players[1]);
Serial.print(", ");
Serial.print(players[2]);
Serial.println("Team 2: ");
Serial.print(players[3]);
Serial.print(", ");
Serial.println(players[4]);
}
void loop(){
}
Would there be a more efficient way?
lloyddean:
Oh, and Happy Anniversary!
True! I didn't even notice that!
#define ARRAY_ENTRIES(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
char players[] = { 'D', 'A', 'E', 'S', 'M' };
void loop()
{}
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(A0));
char swap_temp;
for ( size_t i = ARRAY_ENTRIES(players); i--; )
{
int random_index = random(0, ARRAY_ENTRIES(players));
swap_temp = players[i];
players[i] = players[random_index];
players[random_index] = swap_temp;
}
for ( size_t i = 0; i < ARRAY_ENTRIES(players); i++ )
{
Serial.print(players[i]);
Serial.print(((i < ARRAY_ENTRIES(players)) ? ", " : "\n"));
}
}
I didn't mean that the new array would be "populated with the items in array 1". I meant that the new array would hold index numbers for the locations in the first array. So you would access the data items with something like this
array1[array2[n]]
where n is the nth element in the second array. The value in array2[n] will be a randomized index into array1
There would be no problem ensuring that the array didn't duplicate values just by checking whether the random number is already in the array.
...R
dkl65:
The problem with creating a new array populated with the items in array 1 in random order using only random() is that it might pick the same item twice.