TimMJN:
Just out of curiousity, I wrote 3 different methods for generating random sequences:
method A: for each digit, generate random values until an unused one is found
method B: start with an ordered sequence and swap two random values N times
method C: for each digit, search for an unused value, starting from a random one
#define nNumbers 20
// globals for A
int combinationA[nNumbers];
// globals for B
int combinationB[nNumbers];
// globals for C
int combinationC[nNumbers];
bool hasBeenUsedC[nNumbers];
unsigned long tic;
unsigned long toc;
void setup() {
Serial.begin(9600);
}
void loop() {
tic = millis();
generateCombinationA();
toc = millis();
Serial.print("Elapsed time A: ");
Serial.print(toc - tic);
Serial.println(" ms");
tic = millis();
generateCombinationB();
toc = millis();
Serial.print("Elapsed time B: ");
Serial.print(toc - tic);
Serial.println(" ms");
tic = millis();
generateCombinationC();
toc = millis();
Serial.print("Elapsed time C: ");
Serial.print(toc - tic);
Serial.println(" ms");
delay(50);
}
// generate random sequence by generating number untill an unused digital is found
void generateCombinationA() {
// set first digit
combinationA[0] = random(nNumbers);
//Serial.print("Combination A: ");
//Serial.print(combinationA[0]);
// set next digits
for (int i = 1; i < nNumbers; i++) {
bool hasAlreadyBeenUsed;
do {
hasAlreadyBeenUsed = false;
combinationA[i] = random(nNumbers); // pick a random number
for (int j = 0; j < i; j++) { // loop over previous digits
if (combinationA[i] == combinationA[j]) { // check if its been used already
hasAlreadyBeenUsed = true;
break;
}
}
} while (hasAlreadyBeenUsed); // if so, pick another one
//Serial.print('-');
//Serial.print(combinationA[i]); // if not, print it
} // and continue with the next digit
//Serial.print('\n');
return;
}
// generate random sequence by swapping random pairs of values
void generateCombinationB() {
// initialise as 0-(N-1)
for (int i = 0; i < nNumbers; i++) {
combinationB[i] = i;
}
// preform N swaps
for (int i = 0; i < nNumbers; i++) {
// random indeces
int index1 = random(nNumbers);
int index2 = random(nNumbers);
// get values
int value1 = combinationB[index1];
int value2 = combinationB[index2];
// swap them
combinationB[index1] = value2;
combinationB[index2] = value1;
}
// print
/*
Serial.print("Combination B: ");
Serial.print(combinationB[0]);
for (int i = 1; i < nNumbers; i++) {
Serial.print('-');
Serial.print(combinationB[i]);
}
Serial.print('\n');
*/
}
// generate random sequence by searching for an unused value, starting from a random one
void generateCombinationC() {
// set hasBeenUsed to false
for (int i = 0; i < nNumbers; i++) {
hasBeenUsedC[i] = false;
}
for (int i = 0; i < nNumbers; i++) {
int value = random(nNumbers); // start at a random value
// increment until an unused value is found
while (hasBeenUsedC[value]) {
value = (value + 1) % nNumbers;
}
combinationC[i] = value; //use it in the sequence
hasBeenUsedC[value] = true; // mark the value as used
}
// print
/*
Serial.print("Combination C: ");
Serial.print(combinationC[0]);
for (int i = 1; i < nNumbers; i++) {
Serial.print('-');
Serial.print(combinationC[i]);
}
Serial.print('\n');
*/
}
(all print statements were turned off for fair comparison of the timings)
For short sequences (like the 6 value one here), all methods are basically instant. Interesting stuff happens when generating longer sequences.
Obviously, the brute-force method A gets outpreformed by the others big time. The difference is a factor 5 when generating 200 value sequences.
As it doesn't rely on finding unused values, method B provides a more constant timing. However, its randomness is debateable, as the user has to set the number of swaps. Here, I chose to swap N (N being the number of values in the sequence) times, so that each values gets swapped twice on average.
Method C is very close to B in timing, being up to 20% slower for 200-value sequences. As it relies on searching, it's timings vary much more than B. Also, C requires a second array (of bools) to mark which values have been used, so it is more memory-demanding.
Thank you for listening to my TED talk. <3
that was interesting TED talk.
I guess i should explain my puzzle box in different way.
The idea is that you have 3 things that look the same, with help of other thing you know that one is 3, second is 5 and last one is 7.
Now you have 3 different places to put them into, there is lcd screen that shows number 735, now you have to put these pieces into their places, after doing so you will see number change to lets say 537 and have to change order of these pieces, and so on until you do all of the possible combination.
I wanted to do it in random so person doing it, cant remember the combination, but understad what he is doing.
Without randomness i would do it without problem but that randomness seems to me like more fun.
With that i was thinking how to do thing like this with "random" aspect, and the only think that came to my mind that it would need random combination of 1-6 numbers.
then i would need it to make that number 1 is 357, number 2 is 537, etc, and i have no idea how to use that code generator, how to make it to understand that number 1 is pin2(n2- pin3, n3- pin4, n4- pin5, n5- pin6, n6-pin7), how to make it so if combination is 3 5 2 4 1 6 then i would need to have pin4 high in state2, pin6 in state3, pin3 in state4, pin5 in state5, pin2 in state6 and pin7 in state7.
I hope that now people understand what i got on my mind because i feel like i cant explain what's in my mind and that worrisome