Finding a random position in the unit circle
$begingroup$
Consider the unit circle (with radius $1$, because I can rescale it). Now, choose a random point inside it. How do you do this? The method I'm using is by choosing a random direction, from $0$ to $360$, and then a 'magnitude' variable, from $0$ to $1$. Then, I set the x to the cosine (and the y to the sine), of this direction, multiplied by the magnitude. But this means that, if we consider a ring in this circle representing all points with a magnitude from $0$ to $0.1$, it would have the same likelihood of being selected as the ring with the magnitudes from $0.9$ to $1$, despite $0$-to-$0.1$ having less area. I know that the likelihood of a ring of a single magnitude being selected should be proportional to the magnitude, but how do we express this within an equation?
computer-science circle random
$endgroup$
add a comment |
$begingroup$
Consider the unit circle (with radius $1$, because I can rescale it). Now, choose a random point inside it. How do you do this? The method I'm using is by choosing a random direction, from $0$ to $360$, and then a 'magnitude' variable, from $0$ to $1$. Then, I set the x to the cosine (and the y to the sine), of this direction, multiplied by the magnitude. But this means that, if we consider a ring in this circle representing all points with a magnitude from $0$ to $0.1$, it would have the same likelihood of being selected as the ring with the magnitudes from $0.9$ to $1$, despite $0$-to-$0.1$ having less area. I know that the likelihood of a ring of a single magnitude being selected should be proportional to the magnitude, but how do we express this within an equation?
computer-science circle random
$endgroup$
add a comment |
$begingroup$
Consider the unit circle (with radius $1$, because I can rescale it). Now, choose a random point inside it. How do you do this? The method I'm using is by choosing a random direction, from $0$ to $360$, and then a 'magnitude' variable, from $0$ to $1$. Then, I set the x to the cosine (and the y to the sine), of this direction, multiplied by the magnitude. But this means that, if we consider a ring in this circle representing all points with a magnitude from $0$ to $0.1$, it would have the same likelihood of being selected as the ring with the magnitudes from $0.9$ to $1$, despite $0$-to-$0.1$ having less area. I know that the likelihood of a ring of a single magnitude being selected should be proportional to the magnitude, but how do we express this within an equation?
computer-science circle random
$endgroup$
Consider the unit circle (with radius $1$, because I can rescale it). Now, choose a random point inside it. How do you do this? The method I'm using is by choosing a random direction, from $0$ to $360$, and then a 'magnitude' variable, from $0$ to $1$. Then, I set the x to the cosine (and the y to the sine), of this direction, multiplied by the magnitude. But this means that, if we consider a ring in this circle representing all points with a magnitude from $0$ to $0.1$, it would have the same likelihood of being selected as the ring with the magnitudes from $0.9$ to $1$, despite $0$-to-$0.1$ having less area. I know that the likelihood of a ring of a single magnitude being selected should be proportional to the magnitude, but how do we express this within an equation?
computer-science circle random
computer-science circle random
edited Dec 17 '18 at 15:02
Nathan Skirrow
asked Dec 17 '18 at 10:37
Nathan SkirrowNathan Skirrow
306
306
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers
$endgroup$
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
add a comment |
$begingroup$
If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(rcostheta, rsintheta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $operatorname{f}$, is the inverse of the primitive (indefinite integral) of $operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.
The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $Theta$, $Theta r.dr$.
But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2leq1$, & if it fails discard it: though you are discarding a propotion $1-pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.
$endgroup$
add a comment |
$begingroup$
Let $R$ and $Phi$ be independent random variables
Let $Phi$ have uniform distribution on $[0,2pi)$ or - if you want degrees - on $[0,360)$.
Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=frac{pi r^2}{pi1^2}=r^2$ for $rin[0,1]$ and consequently PDF $f_R(r)=2r$ if $rin[0,1]$ and $0$ otherwise.
Then $(RcosPhi,RsinPhi)$ has uniform distribution on disk ${xinmathbb R^2mid ||x||leq1}$.
The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk ${xinmathbb R^2mid ||x||leq r}$ divided by the area of whole disk ${xinmathbb R^2mid ||x||leq 1}$.
$endgroup$
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%2f3043789%2ffinding-a-random-position-in-the-unit-circle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers
$endgroup$
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
add a comment |
$begingroup$
Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers
$endgroup$
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
add a comment |
$begingroup$
Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers
$endgroup$
Choose a random number $x$ in interval $(-1,1)$. Choose random number $y$ in interval $(-1,1)$. If inside circle, you got a valid point, else choose another set of random numbers
answered Dec 17 '18 at 10:41
AnvitAnvit
1,734419
1,734419
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
add a comment |
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
This is really good, but seems wrong. There's got to be a single mathematical formula that can do this first-time.
$endgroup$
– Nathan Skirrow
Dec 17 '18 at 15:05
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
$begingroup$
@NathanSkirrow This is not "wrong". This will be uniformly distributed. According to other answers, your way is also uniformaly distributed (contrary to your thought) but I'm not sure how they claim that
$endgroup$
– Anvit
Dec 17 '18 at 15:21
add a comment |
$begingroup$
If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(rcostheta, rsintheta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $operatorname{f}$, is the inverse of the primitive (indefinite integral) of $operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.
The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $Theta$, $Theta r.dr$.
But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2leq1$, & if it fails discard it: though you are discarding a propotion $1-pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.
$endgroup$
add a comment |
$begingroup$
If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(rcostheta, rsintheta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $operatorname{f}$, is the inverse of the primitive (indefinite integral) of $operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.
The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $Theta$, $Theta r.dr$.
But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2leq1$, & if it fails discard it: though you are discarding a propotion $1-pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.
$endgroup$
add a comment |
$begingroup$
If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(rcostheta, rsintheta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $operatorname{f}$, is the inverse of the primitive (indefinite integral) of $operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.
The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $Theta$, $Theta r.dr$.
But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2leq1$, & if it fails discard it: though you are discarding a propotion $1-pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.
$endgroup$
If you have uniformly distributed random numbers in [0,1), generate two, multiply one by 2π to get a uniformly distributed angle $theta$; then take the square root of the other, which will give you a radius $r$ with PDF $2r$ in [0,1]; then your point is $$(rcostheta, rsintheta) .$$As a general rule, the function into which a uniformly generated random variable must be fed to yield one with PDF $operatorname{f}$, is the inverse of the primitive (indefinite integral) of $operatorname{f}$. Strictly speaking, this recipe ought to have scaling factors & offsets built into it ... but it's easier just to set them post hoc to what they obviously need to be. In this instance you don't need any atall anyway.
The reason the PDF for $r$ is $2r$ is that the infinitesimally incremental 'catchment' area in a circle is $2πr.dr$ ... or in an arbirary sector of angle $Theta$, $Theta r.dr$.
But you know what - that other method that Anvit gave - just create random points in a unit square & test each for $x^2+y^2leq1$, & if it fails discard it: though you are discarding a propotion $1-pi/4$ of the attempts, there's no need for square root or circular-function calculation; and it probably does have a lower nett consumption of computational resources. Certainly in fact ... in lieu of some ultrasuperslick algorithm for the computation of those functions.
edited Dec 17 '18 at 16:18
Nathan Skirrow
306
306
answered Dec 17 '18 at 11:09
AmbretteOrriseyAmbretteOrrisey
54210
54210
add a comment |
add a comment |
$begingroup$
Let $R$ and $Phi$ be independent random variables
Let $Phi$ have uniform distribution on $[0,2pi)$ or - if you want degrees - on $[0,360)$.
Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=frac{pi r^2}{pi1^2}=r^2$ for $rin[0,1]$ and consequently PDF $f_R(r)=2r$ if $rin[0,1]$ and $0$ otherwise.
Then $(RcosPhi,RsinPhi)$ has uniform distribution on disk ${xinmathbb R^2mid ||x||leq1}$.
The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk ${xinmathbb R^2mid ||x||leq r}$ divided by the area of whole disk ${xinmathbb R^2mid ||x||leq 1}$.
$endgroup$
add a comment |
$begingroup$
Let $R$ and $Phi$ be independent random variables
Let $Phi$ have uniform distribution on $[0,2pi)$ or - if you want degrees - on $[0,360)$.
Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=frac{pi r^2}{pi1^2}=r^2$ for $rin[0,1]$ and consequently PDF $f_R(r)=2r$ if $rin[0,1]$ and $0$ otherwise.
Then $(RcosPhi,RsinPhi)$ has uniform distribution on disk ${xinmathbb R^2mid ||x||leq1}$.
The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk ${xinmathbb R^2mid ||x||leq r}$ divided by the area of whole disk ${xinmathbb R^2mid ||x||leq 1}$.
$endgroup$
add a comment |
$begingroup$
Let $R$ and $Phi$ be independent random variables
Let $Phi$ have uniform distribution on $[0,2pi)$ or - if you want degrees - on $[0,360)$.
Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=frac{pi r^2}{pi1^2}=r^2$ for $rin[0,1]$ and consequently PDF $f_R(r)=2r$ if $rin[0,1]$ and $0$ otherwise.
Then $(RcosPhi,RsinPhi)$ has uniform distribution on disk ${xinmathbb R^2mid ||x||leq1}$.
The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk ${xinmathbb R^2mid ||x||leq r}$ divided by the area of whole disk ${xinmathbb R^2mid ||x||leq 1}$.
$endgroup$
Let $R$ and $Phi$ be independent random variables
Let $Phi$ have uniform distribution on $[0,2pi)$ or - if you want degrees - on $[0,360)$.
Let $R$ takes values in $[0,1]$ with CDF $F_R(r)=frac{pi r^2}{pi1^2}=r^2$ for $rin[0,1]$ and consequently PDF $f_R(r)=2r$ if $rin[0,1]$ and $0$ otherwise.
Then $(RcosPhi,RsinPhi)$ has uniform distribution on disk ${xinmathbb R^2mid ||x||leq1}$.
The CDF of $R$ is based on the fact that the probability that $|X|<r$ must equalize the area of disk ${xinmathbb R^2mid ||x||leq r}$ divided by the area of whole disk ${xinmathbb R^2mid ||x||leq 1}$.
answered Dec 17 '18 at 10:54
drhabdrhab
103k545136
103k545136
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.
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%2f3043789%2ffinding-a-random-position-in-the-unit-circle%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