# Wait, how many permutations are there?

Continuing the discussion from Ryuutama character generator:

Hey folks, I’ve never taken a computer class or college level math class, please help me figure this out. I would like to know the formula to find how many permutations there are from a given array. Because I found the info I said in multiple places, and yet when I count the results there are half as many as I calculated (12 + 12 + 1, rather than 24 + 24 + 1). What’s up with that?!

4 6 6 8 appears twice in your list (second and fourth lines)

ah, so does 4 6 8 6 (third and sixth lines)

i don’t know how to count them when there’s a sum involved. i could do “how many permutations of four slots where 4, 6, or 8 could go in any slot” tho.

something with a 10, like 10 4 4 6, is a possibility yeah? or did i not read your other post carefully enough and you don’t want 10s?

a “set” in math’s “set theory” is a collection of stuff that isn’t repeated and the order doesn’t matter. like, if you wrote “the set of all natural numbers, ascending”, you would be writing forever actually, but as a set, it is exactly the same as someone else writing “the set of all natural numbers, descending.” this is a bad example. i just mean the set of “4, 6, 8” could be written as “6, 8, 4” and still be considered the same set.

you can get into weird shit with sets of polynomials and whatnot!

but for the purposes of what to call “four even numbers that have to add up to 24” where repeats are allowed and order does matter because each number is for a different stat, “set” seems like the sanest word!

1 Like

Yeah, I was flummoxed in the moment and slipped in a generic use of “set”, when I meant permutation. I edited it to be more accurate. This got me looking back at what I had read, to ensure I understood the math correctly, and I dove into the values for factorials. See, aside from finding the actual permutations, I wanted to double-check I knew how many to expect. I understood how `n!` where n is 4 equals 24, as 4 x 3 x 2 x 1 = 24. What I didn’t understand was `(n - n)!`= 1! Or rather, `0! = 1`.

The value of 0! is 1, according to the convention for an empty product.

It goes into it even more at Factorial - Wikipedia.

Okay, so I’m convinced the amount of permutations for each array is 24.

Now it seems whichever way I determined those permutations was off, and I can turn my mind to figuring out a decent way to produce those results.

That make sense to be there, I do not recall which method I used to obtain this list, but they are separate permutations. Imagine them being dice, and there is a green d6 and a yellow d6. They can be slotted into different stats.

This highlights what I’m trying to do, which is make it easy for a human to do something with some numbers (if I were building a list in a form, I’d expect there to be no repeats, as well!). I know think I’ll try a different user interface than what I was considering… but I’m still gonna figure out how to get all possible permutations! Wow, that took longer than I thought… I couldn’t find a simple bash script to take some numbers and spit out permutations. And I sure don’t know enough to do it myself, given the complexity of this particular function. Eventually I found a script at Full Permutation Algorithm Implementation in BASH | Algorithms, Blockchain and Cloud.

``````#!/bin/bash

function swap() {
local string=\$1
local len=\${#string}
local from=\$2
local to=\$3
local i=0
local s=""
while [[ \$i -lt \$len ]]; do
if [[ \$i -eq \$from ]]; then
s=\$s\${string:\$to:1}
elif [[ \$i -eq \$to ]]; then
s=\$s\${string:\$from:1}
else
s=\$s\${string:\$i:1}
fi
i=\$((\$i+1))
done
echo \$s
}

function perm() {
local string=\$1
local len=\${#string}
local idx=\$2
if [[ \$idx -ge \$len ]]; then
echo \$string
else
local i=\$idx
while [[ \$i -lt \$len ]]; do
perm \$(swap \$string \$i \$idx) \$((idx+1))
i=\$((i+1))
done
fi
}

perm \$1
``````

Which gives me:

4668
4686
4668
4686
4866
4866
6468
6486
6648
6684
6864
6846
6648
6684
6468
6486
6846
6864
8664
8646
8664
8646
8466
8466

and

4488
4488
4848
4884
4884
4848
4488
4488
4848
4884
4884
4848
8448
8484
8448
8484
8844
8844
8484
8448
8844
8844
8484
8448

And upon glancing at those numbers I know now it makes no sense to list all these numbers in a form… even de-duplicated those are too many options for my purpose. I stopped working on that character creator form, but I didn’t stop thinking about how to ask this question using an accessible, language-driven web form. I think I might do it in three or less questions (paraphrased):

1. Does your character have Average (6 6 6 6), Standard (4 6 6 8), or Specialized (4 4 8 8) stats?
b. Standard: ask 2 and 3.
c. Specialized ask 2 and 4.

2. Which stat will have 8?

3. Which stat will have 4?

4. Which other stat will have 8?

Since a player wanting to change their stats from Average likely know why, they can answer those simple questions and we know the array. One could use the same method without revealing the numbers, for a particular type of character creation experience.

Which aspect of your character is above average? Below average?