getting 2 random numbers to add up to less than number nIs this number random?Easy way to generate random numbers?Pseudo Random Number Generation on the GPUCompressing random numbersWhat is the statistical difference (if any) between these two methods of generating an n-digit random number?What constitutes randomness?generate random number from exponential distribution with random right truncationFast method to pick unique random numbers?Is the sequence of non repeating random numbers still random?What does “less random” mean?

Is it canonical bit space?

How to take photos in burst mode, without vibration?

How can I tell someone that I want to be his or her friend?

Could gravitational lensing be used to protect a spaceship from a laser?

Blender 2.8 I can't see vertices, edges or faces in edit mode

Can I use a neutral wire from another outlet to repair a broken neutral?

Assassin's bullet with mercury

How do conventional missiles fly?

Why is Collection not simply treated as Collection<?>

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

What killed these X2 caps?

Why is the ratio of two extensive quantities always intensive?

Why doesn't H₄O²⁺ exist?

How could indestructible materials be used in power generation?

Can one be a co-translator of a book, if he does not know the language that the book is translated into?

Took a trip to a parallel universe, need help deciphering

Arrow those variables!

Why is the 'in' operator throwing an error with a string literal instead of logging false?

In Romance of the Three Kingdoms why do people still use bamboo sticks when papers are already invented?

How badly should I try to prevent a user from XSSing themselves?

90's TV series where a boy goes to another dimension through portal near power lines

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?

In a Spin are Both Wings Stalled?



getting 2 random numbers to add up to less than number n


Is this number random?Easy way to generate random numbers?Pseudo Random Number Generation on the GPUCompressing random numbersWhat is the statistical difference (if any) between these two methods of generating an n-digit random number?What constitutes randomness?generate random number from exponential distribution with random right truncationFast method to pick unique random numbers?Is the sequence of non repeating random numbers still random?What does “less random” mean?













0












$begingroup$


So i'm trying to code a game for context. I'd like to be able to get 2 random numbers(a, b) that will add up to less then number n.



Here's my initial approach.



I get a random number between 0 and n.



Lets says n = 100 in an example. And my first random number is 78.



so i would take the remainder, in this case 22 and find a random number between 0 and 22. And then it would spit out some random number.



I can't seem to think of an approach where the second number gets as much "randomness" opportunity as the first number, due to the range being reduced.



What are some more approaches to getting 2 random numbers that must be less then sum n?










share|cite|improve this question









New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    i guess i could also find 2 random numbers from 0 and n / 2.
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:16










  • $begingroup$
    so sorry, edited!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:23










  • $begingroup$
    Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
    $endgroup$
    – David G. Stork
    Mar 29 at 0:44















0












$begingroup$


So i'm trying to code a game for context. I'd like to be able to get 2 random numbers(a, b) that will add up to less then number n.



Here's my initial approach.



I get a random number between 0 and n.



Lets says n = 100 in an example. And my first random number is 78.



so i would take the remainder, in this case 22 and find a random number between 0 and 22. And then it would spit out some random number.



I can't seem to think of an approach where the second number gets as much "randomness" opportunity as the first number, due to the range being reduced.



What are some more approaches to getting 2 random numbers that must be less then sum n?










share|cite|improve this question









New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    i guess i could also find 2 random numbers from 0 and n / 2.
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:16










  • $begingroup$
    so sorry, edited!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:23










  • $begingroup$
    Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
    $endgroup$
    – David G. Stork
    Mar 29 at 0:44













0












0








0





$begingroup$


So i'm trying to code a game for context. I'd like to be able to get 2 random numbers(a, b) that will add up to less then number n.



Here's my initial approach.



I get a random number between 0 and n.



Lets says n = 100 in an example. And my first random number is 78.



so i would take the remainder, in this case 22 and find a random number between 0 and 22. And then it would spit out some random number.



I can't seem to think of an approach where the second number gets as much "randomness" opportunity as the first number, due to the range being reduced.



What are some more approaches to getting 2 random numbers that must be less then sum n?










share|cite|improve this question









New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




So i'm trying to code a game for context. I'd like to be able to get 2 random numbers(a, b) that will add up to less then number n.



Here's my initial approach.



I get a random number between 0 and n.



Lets says n = 100 in an example. And my first random number is 78.



so i would take the remainder, in this case 22 and find a random number between 0 and 22. And then it would spit out some random number.



I can't seem to think of an approach where the second number gets as much "randomness" opportunity as the first number, due to the range being reduced.



What are some more approaches to getting 2 random numbers that must be less then sum n?







algebra-precalculus statistics random






share|cite|improve this question









New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|cite|improve this question









New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this question




share|cite|improve this question








edited Mar 29 at 1:34









J. W. Tanner

4,4711320




4,4711320






New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 29 at 0:09









Andrew KimAndrew Kim

1033




1033




New contributor




Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Andrew Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • $begingroup$
    i guess i could also find 2 random numbers from 0 and n / 2.
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:16










  • $begingroup$
    so sorry, edited!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:23










  • $begingroup$
    Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
    $endgroup$
    – David G. Stork
    Mar 29 at 0:44
















  • $begingroup$
    i guess i could also find 2 random numbers from 0 and n / 2.
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:16










  • $begingroup$
    so sorry, edited!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:23










  • $begingroup$
    Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
    $endgroup$
    – David G. Stork
    Mar 29 at 0:44















$begingroup$
i guess i could also find 2 random numbers from 0 and n / 2.
$endgroup$
– Andrew Kim
Mar 29 at 0:16




$begingroup$
i guess i could also find 2 random numbers from 0 and n / 2.
$endgroup$
– Andrew Kim
Mar 29 at 0:16












$begingroup$
so sorry, edited!
$endgroup$
– Andrew Kim
Mar 29 at 0:23




$begingroup$
so sorry, edited!
$endgroup$
– Andrew Kim
Mar 29 at 0:23












$begingroup$
Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
$endgroup$
– David G. Stork
Mar 29 at 0:44




$begingroup$
Definitely not the way to go... highly non-random. Your method never yield $94$ and $2$, for example, even though it should.
$endgroup$
– David G. Stork
Mar 29 at 0:44










2 Answers
2






active

oldest

votes


















1












$begingroup$

This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.



  • Choose an integer $x$ randomly in the range $1$ to $n+1$.



  • Choose an integer $y$ randomly in the range $1$ to $n$.



    • If $yge x$, replace $y$ with $y+1$.


  • Let $a=min(x,y)-1$, and let $b=max(x,y)-min(x,y)-1$.






share|cite|improve this answer









$endgroup$












  • $begingroup$
    i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:56


















3












$begingroup$

We might as well take $n=1$. You can multiply the numbers that come from this by $n$ to get the range you want.



You are trying to sample the triangle $a ge 0, b ge 0, a+b le 1$ uniformly. This triangle is half of the square $0 le a le 1, 0 le b le 1$, which is easy to sample-just draw $a$ and $b$ uniformly from $[0,1]$. Now if $a+b gt 1$, we can fold over the diagonal. Instead of $a,b$, use $1-a,1-b$ for your variables. The sum is $2-a-b$, which is less than $1$ because $a+b gt 1$.



$a$ and $b$ have the same distribution, which is a triangle with density $2$ at $0$ falling linearly to $0$ at $1$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    how does that compare to the approach in my comment? are they both equally random?
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:24











  • $begingroup$
    whoops, sorry, saw your comment, less than is what i meant =(
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:25










  • $begingroup$
    I have replaced the answer with one that does what you want.
    $endgroup$
    – Ross Millikan
    Mar 29 at 0:29











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
);



);






Andrew Kim is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3166572%2fgetting-2-random-numbers-to-add-up-to-less-than-number-n%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









1












$begingroup$

This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.



  • Choose an integer $x$ randomly in the range $1$ to $n+1$.



  • Choose an integer $y$ randomly in the range $1$ to $n$.



    • If $yge x$, replace $y$ with $y+1$.


  • Let $a=min(x,y)-1$, and let $b=max(x,y)-min(x,y)-1$.






share|cite|improve this answer









$endgroup$












  • $begingroup$
    i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:56















1












$begingroup$

This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.



  • Choose an integer $x$ randomly in the range $1$ to $n+1$.



  • Choose an integer $y$ randomly in the range $1$ to $n$.



    • If $yge x$, replace $y$ with $y+1$.


  • Let $a=min(x,y)-1$, and let $b=max(x,y)-min(x,y)-1$.






share|cite|improve this answer









$endgroup$












  • $begingroup$
    i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:56













1












1








1





$begingroup$

This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.



  • Choose an integer $x$ randomly in the range $1$ to $n+1$.



  • Choose an integer $y$ randomly in the range $1$ to $n$.



    • If $yge x$, replace $y$ with $y+1$.


  • Let $a=min(x,y)-1$, and let $b=max(x,y)-min(x,y)-1$.






share|cite|improve this answer









$endgroup$



This method selects two random nonnegative integers $a$ and $b$ so that $a+b< n$. This method is perfect in the sense that every possible ordered pair $(a,b)$ is equally likely. This means that $a$ and $b$ will have the exact same degree of randomness.



  • Choose an integer $x$ randomly in the range $1$ to $n+1$.



  • Choose an integer $y$ randomly in the range $1$ to $n$.



    • If $yge x$, replace $y$ with $y+1$.


  • Let $a=min(x,y)-1$, and let $b=max(x,y)-min(x,y)-1$.







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Mar 29 at 0:32









Mike EarnestMike Earnest

26.9k22152




26.9k22152











  • $begingroup$
    i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:56
















  • $begingroup$
    i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:56















$begingroup$
i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
$endgroup$
– Andrew Kim
Mar 29 at 0:56




$begingroup$
i don't know enough math to understand the other answer unfortunately, I like this answer because I can easily translate it to code. And it seems to be working. Thanks so much!
$endgroup$
– Andrew Kim
Mar 29 at 0:56











3












$begingroup$

We might as well take $n=1$. You can multiply the numbers that come from this by $n$ to get the range you want.



You are trying to sample the triangle $a ge 0, b ge 0, a+b le 1$ uniformly. This triangle is half of the square $0 le a le 1, 0 le b le 1$, which is easy to sample-just draw $a$ and $b$ uniformly from $[0,1]$. Now if $a+b gt 1$, we can fold over the diagonal. Instead of $a,b$, use $1-a,1-b$ for your variables. The sum is $2-a-b$, which is less than $1$ because $a+b gt 1$.



$a$ and $b$ have the same distribution, which is a triangle with density $2$ at $0$ falling linearly to $0$ at $1$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    how does that compare to the approach in my comment? are they both equally random?
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:24











  • $begingroup$
    whoops, sorry, saw your comment, less than is what i meant =(
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:25










  • $begingroup$
    I have replaced the answer with one that does what you want.
    $endgroup$
    – Ross Millikan
    Mar 29 at 0:29















3












$begingroup$

We might as well take $n=1$. You can multiply the numbers that come from this by $n$ to get the range you want.



You are trying to sample the triangle $a ge 0, b ge 0, a+b le 1$ uniformly. This triangle is half of the square $0 le a le 1, 0 le b le 1$, which is easy to sample-just draw $a$ and $b$ uniformly from $[0,1]$. Now if $a+b gt 1$, we can fold over the diagonal. Instead of $a,b$, use $1-a,1-b$ for your variables. The sum is $2-a-b$, which is less than $1$ because $a+b gt 1$.



$a$ and $b$ have the same distribution, which is a triangle with density $2$ at $0$ falling linearly to $0$ at $1$.






share|cite|improve this answer











$endgroup$












  • $begingroup$
    how does that compare to the approach in my comment? are they both equally random?
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:24











  • $begingroup$
    whoops, sorry, saw your comment, less than is what i meant =(
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:25










  • $begingroup$
    I have replaced the answer with one that does what you want.
    $endgroup$
    – Ross Millikan
    Mar 29 at 0:29













3












3








3





$begingroup$

We might as well take $n=1$. You can multiply the numbers that come from this by $n$ to get the range you want.



You are trying to sample the triangle $a ge 0, b ge 0, a+b le 1$ uniformly. This triangle is half of the square $0 le a le 1, 0 le b le 1$, which is easy to sample-just draw $a$ and $b$ uniformly from $[0,1]$. Now if $a+b gt 1$, we can fold over the diagonal. Instead of $a,b$, use $1-a,1-b$ for your variables. The sum is $2-a-b$, which is less than $1$ because $a+b gt 1$.



$a$ and $b$ have the same distribution, which is a triangle with density $2$ at $0$ falling linearly to $0$ at $1$.






share|cite|improve this answer











$endgroup$



We might as well take $n=1$. You can multiply the numbers that come from this by $n$ to get the range you want.



You are trying to sample the triangle $a ge 0, b ge 0, a+b le 1$ uniformly. This triangle is half of the square $0 le a le 1, 0 le b le 1$, which is easy to sample-just draw $a$ and $b$ uniformly from $[0,1]$. Now if $a+b gt 1$, we can fold over the diagonal. Instead of $a,b$, use $1-a,1-b$ for your variables. The sum is $2-a-b$, which is less than $1$ because $a+b gt 1$.



$a$ and $b$ have the same distribution, which is a triangle with density $2$ at $0$ falling linearly to $0$ at $1$.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Mar 29 at 0:28

























answered Mar 29 at 0:20









Ross MillikanRoss Millikan

301k24200375




301k24200375











  • $begingroup$
    how does that compare to the approach in my comment? are they both equally random?
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:24











  • $begingroup$
    whoops, sorry, saw your comment, less than is what i meant =(
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:25










  • $begingroup$
    I have replaced the answer with one that does what you want.
    $endgroup$
    – Ross Millikan
    Mar 29 at 0:29
















  • $begingroup$
    how does that compare to the approach in my comment? are they both equally random?
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:24











  • $begingroup$
    whoops, sorry, saw your comment, less than is what i meant =(
    $endgroup$
    – Andrew Kim
    Mar 29 at 0:25










  • $begingroup$
    I have replaced the answer with one that does what you want.
    $endgroup$
    – Ross Millikan
    Mar 29 at 0:29















$begingroup$
how does that compare to the approach in my comment? are they both equally random?
$endgroup$
– Andrew Kim
Mar 29 at 0:24





$begingroup$
how does that compare to the approach in my comment? are they both equally random?
$endgroup$
– Andrew Kim
Mar 29 at 0:24













$begingroup$
whoops, sorry, saw your comment, less than is what i meant =(
$endgroup$
– Andrew Kim
Mar 29 at 0:25




$begingroup$
whoops, sorry, saw your comment, less than is what i meant =(
$endgroup$
– Andrew Kim
Mar 29 at 0:25












$begingroup$
I have replaced the answer with one that does what you want.
$endgroup$
– Ross Millikan
Mar 29 at 0:29




$begingroup$
I have replaced the answer with one that does what you want.
$endgroup$
– Ross Millikan
Mar 29 at 0:29










Andrew Kim is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Andrew Kim is a new contributor. Be nice, and check out our Code of Conduct.












Andrew Kim is a new contributor. Be nice, and check out our Code of Conduct.











Andrew Kim is a new contributor. Be nice, and check out our Code of Conduct.














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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3166572%2fgetting-2-random-numbers-to-add-up-to-less-than-number-n%23new-answer', 'question_page');

);

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







Popular posts from this blog

Boston (Lincolnshire) Stedsbyld | Berne yn Boston | NavigaasjemenuBoston Borough CouncilBoston, Lincolnshire

Trouble understanding the speech of overseas colleaguesHow can I better understand manager or clients with strong accents?Adding more movement and speech at the fundamental level to a highly-sedentary job?Difficulty in understanding Manager's accent(language and communication)How to adjust yourself where your colleagues are not understanding to you?Understanding manager's expectationsForeigner and colleagues using slangHaving difficulty understanding meetingsHow do you breathe when giving a speech?Trouble Waking Up for Emergencies (On-Call)Problems with colleaguesColleagues feeling insecure when I do my work

Is the concept of a “numerable” fiber bundle really useful or an empty generalization?Non trivial vector bundle over non-paracompact contractible spaceExample of fiber bundle that is not a fibrationGlobalising fibrations by schedulesFiber bundle = principal bundle + fiber?Numerable covers from the point of view of Grothendieck topologiesGlobal sections for torus fiber bundleAre there analogs of smooth partitions of unity and good open covers for PL-manifolds?Two natural maps asssociated with the nerve of a coverDescent theory, fibrations, and bundlesIn which sense are Euler-Lagrange PDE's on fiber bundles quasi-linear?What is the local structure of a fibration?Complete proof of Homotopy invariance of a numerable fiber bundle based on CHPLocally trivial fibration over a suspension