Permutations without repeating myself
This may be a quite simple question, but there we go.
Imagine that I've got a sequence $s = [s_1, s_2, dots, s_n]$. I want to generate programatically a new random permutation $s'$ of $s$ so that $forall i in [1,n],; s[i] neq s'[i]$, of, what is the same, none of the values of the new permutation coincide 1-1 with the values of the previous one.
For instance, for $s = [1,2,3,4]$, $s' = [2,3,4,1]$ is a desired permutation because $s'$ is a permutation of $s$ and $2 neq 1$, $2neq 3$, and so on.
I know that the Fiser-Yates produces random permutation, but you cannot force the requirement that $s[i] neq s'[i]$ for all $i$'s.
Thank you.
Edit: apparently I'm asking for how to generate a derangement with uniform probability distribution.
algorithms permutations
add a comment |
This may be a quite simple question, but there we go.
Imagine that I've got a sequence $s = [s_1, s_2, dots, s_n]$. I want to generate programatically a new random permutation $s'$ of $s$ so that $forall i in [1,n],; s[i] neq s'[i]$, of, what is the same, none of the values of the new permutation coincide 1-1 with the values of the previous one.
For instance, for $s = [1,2,3,4]$, $s' = [2,3,4,1]$ is a desired permutation because $s'$ is a permutation of $s$ and $2 neq 1$, $2neq 3$, and so on.
I know that the Fiser-Yates produces random permutation, but you cannot force the requirement that $s[i] neq s'[i]$ for all $i$'s.
Thank you.
Edit: apparently I'm asking for how to generate a derangement with uniform probability distribution.
algorithms permutations
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22
add a comment |
This may be a quite simple question, but there we go.
Imagine that I've got a sequence $s = [s_1, s_2, dots, s_n]$. I want to generate programatically a new random permutation $s'$ of $s$ so that $forall i in [1,n],; s[i] neq s'[i]$, of, what is the same, none of the values of the new permutation coincide 1-1 with the values of the previous one.
For instance, for $s = [1,2,3,4]$, $s' = [2,3,4,1]$ is a desired permutation because $s'$ is a permutation of $s$ and $2 neq 1$, $2neq 3$, and so on.
I know that the Fiser-Yates produces random permutation, but you cannot force the requirement that $s[i] neq s'[i]$ for all $i$'s.
Thank you.
Edit: apparently I'm asking for how to generate a derangement with uniform probability distribution.
algorithms permutations
This may be a quite simple question, but there we go.
Imagine that I've got a sequence $s = [s_1, s_2, dots, s_n]$. I want to generate programatically a new random permutation $s'$ of $s$ so that $forall i in [1,n],; s[i] neq s'[i]$, of, what is the same, none of the values of the new permutation coincide 1-1 with the values of the previous one.
For instance, for $s = [1,2,3,4]$, $s' = [2,3,4,1]$ is a desired permutation because $s'$ is a permutation of $s$ and $2 neq 1$, $2neq 3$, and so on.
I know that the Fiser-Yates produces random permutation, but you cannot force the requirement that $s[i] neq s'[i]$ for all $i$'s.
Thank you.
Edit: apparently I'm asking for how to generate a derangement with uniform probability distribution.
algorithms permutations
algorithms permutations
edited Nov 29 '18 at 14:23
JnxF
asked Nov 29 '18 at 13:03
JnxFJnxF
1,0691821
1,0691821
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22
add a comment |
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22
add a comment |
2 Answers
2
active
oldest
votes
One way to generate a random derangement $sigma$ of a set $S$ of $n$ letters is based on the recursion $!n = (n-1)(!(n-1) + !(n-2)$. First compute the derangement numbers $!k$ for $1 le k le n$.
Choose $sigma(s_1)$ uniformly from $S backslash {s_1}$. With probability $!(n-2)/(!(n-1)+!(n-2))$, let $sigma(sigma(s_1)) = s_1$, and the rest of $sigma$ is a derangement of the remaining $n-2$
letters $S backslash {s_1, sigma(s_1)}$. Otherwise, recursively take a derangement of
$tau$ of $S backslash {s_1}$; let $sigma(tau^{-1}(sigma(s_1))) = s_1$, otherwise $sigma(i) = tau(i)$.
add a comment |
Generate a random derangement (that's a permutation with no fixed points), then multiply it by $s$ to obtain $s'$.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3018594%2fpermutations-without-repeating-myself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way to generate a random derangement $sigma$ of a set $S$ of $n$ letters is based on the recursion $!n = (n-1)(!(n-1) + !(n-2)$. First compute the derangement numbers $!k$ for $1 le k le n$.
Choose $sigma(s_1)$ uniformly from $S backslash {s_1}$. With probability $!(n-2)/(!(n-1)+!(n-2))$, let $sigma(sigma(s_1)) = s_1$, and the rest of $sigma$ is a derangement of the remaining $n-2$
letters $S backslash {s_1, sigma(s_1)}$. Otherwise, recursively take a derangement of
$tau$ of $S backslash {s_1}$; let $sigma(tau^{-1}(sigma(s_1))) = s_1$, otherwise $sigma(i) = tau(i)$.
add a comment |
One way to generate a random derangement $sigma$ of a set $S$ of $n$ letters is based on the recursion $!n = (n-1)(!(n-1) + !(n-2)$. First compute the derangement numbers $!k$ for $1 le k le n$.
Choose $sigma(s_1)$ uniformly from $S backslash {s_1}$. With probability $!(n-2)/(!(n-1)+!(n-2))$, let $sigma(sigma(s_1)) = s_1$, and the rest of $sigma$ is a derangement of the remaining $n-2$
letters $S backslash {s_1, sigma(s_1)}$. Otherwise, recursively take a derangement of
$tau$ of $S backslash {s_1}$; let $sigma(tau^{-1}(sigma(s_1))) = s_1$, otherwise $sigma(i) = tau(i)$.
add a comment |
One way to generate a random derangement $sigma$ of a set $S$ of $n$ letters is based on the recursion $!n = (n-1)(!(n-1) + !(n-2)$. First compute the derangement numbers $!k$ for $1 le k le n$.
Choose $sigma(s_1)$ uniformly from $S backslash {s_1}$. With probability $!(n-2)/(!(n-1)+!(n-2))$, let $sigma(sigma(s_1)) = s_1$, and the rest of $sigma$ is a derangement of the remaining $n-2$
letters $S backslash {s_1, sigma(s_1)}$. Otherwise, recursively take a derangement of
$tau$ of $S backslash {s_1}$; let $sigma(tau^{-1}(sigma(s_1))) = s_1$, otherwise $sigma(i) = tau(i)$.
One way to generate a random derangement $sigma$ of a set $S$ of $n$ letters is based on the recursion $!n = (n-1)(!(n-1) + !(n-2)$. First compute the derangement numbers $!k$ for $1 le k le n$.
Choose $sigma(s_1)$ uniformly from $S backslash {s_1}$. With probability $!(n-2)/(!(n-1)+!(n-2))$, let $sigma(sigma(s_1)) = s_1$, and the rest of $sigma$ is a derangement of the remaining $n-2$
letters $S backslash {s_1, sigma(s_1)}$. Otherwise, recursively take a derangement of
$tau$ of $S backslash {s_1}$; let $sigma(tau^{-1}(sigma(s_1))) = s_1$, otherwise $sigma(i) = tau(i)$.
answered Nov 29 '18 at 14:48
Robert IsraelRobert Israel
319k23208458
319k23208458
add a comment |
add a comment |
Generate a random derangement (that's a permutation with no fixed points), then multiply it by $s$ to obtain $s'$.
add a comment |
Generate a random derangement (that's a permutation with no fixed points), then multiply it by $s$ to obtain $s'$.
add a comment |
Generate a random derangement (that's a permutation with no fixed points), then multiply it by $s$ to obtain $s'$.
Generate a random derangement (that's a permutation with no fixed points), then multiply it by $s$ to obtain $s'$.
answered Nov 29 '18 at 13:09
Alexander BursteinAlexander Burstein
1,129217
1,129217
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3018594%2fpermutations-without-repeating-myself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Two sequences can be different while representing the same permutation. I am not sure about what you are asking.
– nicomezi
Nov 29 '18 at 13:13
So the question is how to generate a derangement with uniform probability distribution?
– Peter Taylor
Nov 29 '18 at 14:21
@PeterTaylor exactly
– JnxF
Nov 29 '18 at 14:22