how do i calculate the total number of ways a series of numbers can be rearranged? numbers cannot be repeated
so 1 and 2 could be
1 then 2 or
2 then 1
but not 1 then 1 or 2 then 2
the set in question is 1,2,3,4,5,6,7,8
?
Thanks
how do i calculate the total number of ways a series of numbers can be rearranged? numbers cannot be repeated
so 1 and 2 could be
1 then 2 or
2 then 1
but not 1 then 1 or 2 then 2
the set in question is 1,2,3,4,5,6,7,8
?
Thanks
Is the Arduino going to be doing the arranging? This is http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl
not
http://www.helpmewithmyhomework.cc/cgi-bin/yabb2/YaBB.pl
Lol! Yes the Arduino is going to be doing it. Specifically it's going to be playing back an audio loop spliced into eight parts in various random patterns.
I'm 39, I don't have homework anymore
What you are talking about is Permutations.
There are 2 permutations of two items.
6 permutations of 3 items.
The way you calculate it is called the factorial and is the number multiplied by all the numbers that precede it.
so 2 factorial is 2 * 1 = 2
3 factorial is 3 * 2 * 1 = 6
4 factorial is 4 * 3 * 2 * 1 = 24
and so on.
Remember that the largest factorial you can represent in an Arduino 'int' is 7!, and in an unsigned int 8!
Thanks all
8! is 40,320
I was hoping it's square root would be a whole number but it isn't as then I could have two pots to select the entire range of 40,320 possible permutations.
So two pots connected to the analog pins will in fact select values in the range 0-255 or, when multiplied, 0-65,535. I guess I could just cycle round ...
What are you trying to do exactly?
I could have two pots to select the entire range of 40,320 possible permutations.
This presumes that you can actually get every value from 0 to 1023 (or at least one of every group of 4 values) from a potentiometer. I wouldn't bet a nickel on that with most potentiometers.
But, suppose you did. Do you propose to use that index to look up a value in an array to know what tracks to play in what order?
I'm playing around with (audio) sample playback using an Arduino (as various others have)
A common trick with sample based music is to cut a loop into a number of parts (8 typically) and rearrange those parts into new sequences to make variations.
I want to map those possibilities to as few a controls as possible. A single pot wouldn't have sufficient range (0-255) and a rotary encoder would be tedious (too many turns) so I imagined two pots. So values are derived as in pot 1 x pot 2 ...
... which begs the question of how I turn a number in the given range to one of the actual permutations ... hmmm (?)
(Where the hell is the "quote" button?!)
Sorry is my memory failing me, the analog ins read 0-1023 not 0-255?
I guess using an array would work but I'm probably going to run out of memory on an Arduino aren't I? I was hoping I could "reverse a factorial" ... whatever that means!
Accuracy isn't a massive deal, if I didn't get every value in the range so be it. This is intended as something to play with rather than an accurate compositional tool. That said random would not be right either as there needs to be a sense of whatever it's doing it does it consistently
Yes, analogRead returns a value between 0 and 1023.
I guess using an array would work but I'm probably going to run out of memory on an Arduino aren't I?
I was hoping I could "reverse a factorial" ... whatever that means!
The number of permutations (8!) tells you nothing about what those permutations are. You are trying to select one value from an undefined set of values.
The values are playback order
1st then 2nd then 3rd then 4th
or indeed
2nd then 1st then 4th then 3rd
(for 4! at any rate)
but yeah ... how do i translate a value in the given range (which is the total permutations in the set) to a specific permutation ... how do I even derive them if I did use a look up table ... cripes ... interesting stuff.
You need to separate the problems of selecting a permutation and then mapping a permutation number to a sequence.
You are not going to map it with storage. It has to be a mathematical function.
To select the permutation in a repeatable way, (is that a requirement?) then what about a keypad and LCD display?
Dial-a-combination
It will take 3 bits per position to record which track to play next. Since there are 8 tracks to play in some order, that's 24 bits per permutation.
3 bytes per permutation * 40,320 permutations is 120, 960 bytes. I don't think the Arduino has that many bytes of memory.
So, the lookup table idea won't work.
The only easily repeatable process I can think of is to use 8 rotary encoders to select the track to play in the nth position. As the encoder rotates, use the modulo function to keep the value in the 0 to 7 range.
This wouldn't preclude playing the same track twice, though.
Yes all helpful points chaps thanks.
The main requirement is a "minimum" interface. This function is only one of several others. I like the idea of exploring how far you can push an interface before it becomes meaningless yet remains fun. The purpose of the interface isn't so much "dial-a-permutation" but just "dial" and see what happens with the single proviso that turning it to the left today does the same thing as turning it to the left tomorrow - if you see what I mean.
Perhaps the answer is to limit the set ... but to what?
OK, how about this.
Lets make our life easy and have three pots, and take the most significant 8 bits from each.
That gives us 24 bits from which to make a unique sequence.
Lets split that into 3 bits for the sequence position for each clip. We can have a positions array[8].
BUT, how do we avoid duplicates in the sequence? Well, we assign the sequences naively using this approach and then:
Iterate over the positions array, and if any number is the same as a number earlier in the sequence, then bump it up with wrap around to zero, until you find a free position in which to play that clip.
I have no proof that this would cover all the permutations, and there may well be some flaw in it that is obvious to someone who knows maths but I'd be tempted to find out by coding it, as its pretty simple
@Si
Yeah I think that has some mileage, I'll try it. Thanks.
Good luck - let us know how it goes - thanks for posting such an interesting problem.
Permutations, long time ago, I looked in my archive and found a permutation algorithm; was written in TurboC 2.0 so a port was needed. It resulted in the code below, it is maybe not the fastest but in generates all permutations of a string of length 10 in ~56 seconds (exclusive printing).
char permstring[12] = "123456789A";
void permutate(int n)
{
if (n==0) // end reached print the string
{
//for (int i = 0; i < strlen(permstring); i++)
// Serial.print(permstring[i]);
//Serial.println();
return;
}
for (int i = 0; i <n; i++)
{
// swap
char t = permstring[i];
permstring[i] = permstring[n-1];
permstring[n-1] = t;
// permutate substrings
permutate(n-1);
// swap back
t = permstring[i];
permstring[i] = permstring[n-1];
permstring[n-1] = t;
}
}
void setup()
{
Serial.begin(115200);
unsigned long time = millis();
permutate(strlen(permstring));
time = millis() - time;
Serial.print("TIME: ");
Serial.println(time);
while(1);
}
void loop()
{
}
There exist an algorithm to find directly the n=th permutation (google => found) need a port to Arduino. I'll be back