# Generate N random numbers with a given probability and condition

13 messages
Open this post in threaded view
|

## Generate N random numbers with a given probability and condition

 Dear all, I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this. For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested: x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1)))) But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)). Many thanks for your time Nell         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

 This looks like homework (which is off topic here per the Posting Guide). Also, please send your emails in plain text format to avoid us seeing your message differently than you do. On July 4, 2018 3:21:34 PM PDT, Nelly Reduan <[hidden email]> wrote: >Dear all, > >I would like to generate N random numbers with a given probability and >condition but I'm not sure how to do this. >For example, I have N = 20 and the vector from which to choose is >seq(0, 10, 1). I have tested: > >x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, >times=length(seq(0, 10, 1)))) > >But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)). >Many thanks for your time >Nell > > > [[alternative HTML version deleted]] -- Sent from my phone. Please excuse my brevity. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

 In reply to this post by Nelly Reduan On 05/07/18 10:21, Nelly Reduan wrote: > Dear all, > > I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this. > For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested: > > x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1)))) > > But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)). > Many thanks for your time. Your thinking requires considerable clarification. (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10. (2) Hence length(seq(0,10,1)) is 11. (3) Likewise max(seq(0,10,1)) is 10. (4) Your prob vector is *constant* --- so specifying "prob" makes      no difference --- the result is the same as if you omitted "prob". (5) You need to think carefully about what you really mean by "random".      In what way do you want the final result to be "random"? I expect that the lecturer who assigned this problem to you  needs to clarify his/her thinking as well. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

 Thank you very much for your reply. By omitting the probability, the expected results could be: c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0) c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0) If I omit the probability, I would like to generate N random positive integers that sum to M and the integers would be selected from a uniform distribution. Many thanks for your time Nell ________________________________ De : Rolf Turner <[hidden email]> Envoyé : mercredi 4 juillet 2018 16:11:11 À : Nelly Reduan Cc : [hidden email] Objet : Re: [R] Generate N random numbers with a given probability and condition On 05/07/18 10:21, Nelly Reduan wrote: > Dear all, > > I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this. > For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested: > > x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1)))) > > But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)). > Many thanks for your time. Your thinking requires considerable clarification. (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10. (2) Hence length(seq(0,10,1)) is 11. (3) Likewise max(seq(0,10,1)) is 10. (4) Your prob vector is *constant* --- so specifying "prob" makes      no difference --- the result is the same as if you omitted "prob". (5) You need to think carefully about what you really mean by "random".      In what way do you want the final result to be "random"? I expect that the lecturer who assigned this problem to you  needs to clarify his/her thinking as well. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

Open this post in threaded view
|

## Re: [FORGED] Re: Generate N random numbers with a given probability and condition

 In reply to this post by Jim Lemon-4 On 11/07/18 12:46, Jim Lemon wrote: > Hi Nell, > I may not have the right idea about this, but I think you need to do > this in two steps if it can be done. Let's say you want a sequence of > 20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate > the monotonically increasing sequences like this: > > c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1) > c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2) > ... > c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10) Jim:  You should *not* do people's homework for them! cheers, Rolf -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: [FORGED] Re: Generate N random numbers with a given probability and condition

 Wasn't there also the requirement that the numbers be drawn from a uniform distribution? These sequences are not. I wonder whether this can for all practical purposes be simplified to consider only the sequence with maximum entropy. B. > On 2018-07-11, at 06:23, Rolf Turner <[hidden email]> wrote: > > On 11/07/18 12:46, Jim Lemon wrote: >> Hi Nell, >> I may not have the right idea about this, but I think you need to do >> this in two steps if it can be done. Let's say you want a sequence of >> 20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate >> the monotonically increasing sequences like this: >> c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1) >> c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2) >> ... >> c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10) > > > > Jim:  You should *not* do people's homework for them! > > cheers, > > Rolf > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

Open this post in threaded view
|

## Re: Generate N random numbers with a given probability and condition

 In reply to this post by Nelly Reduan On 04/07/2018 6:21 PM, Nelly Reduan wrote: > Dear all, > > I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this. > For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested: > > x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1)))) > > But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)). > Many thanks for your time I'd recommend an MCMC solution to this problem.  Set up a distribution that is uniform on vectors that satisfy the conditions, with penalties on vectors that don't.  Use the Metropolis algorithm with proposals that pick a pair of entries and increase one, decrease the other, then let MCMC run.  At the end, filter out the cases that violate the conditions. The hard part is knowing how long to let it run for a satisfactory sample, and how correlated later draws will be.  Propp and Wilson's perfect sampling algorithm might allow an exact draw, though I don't quite see how, and I'm not sure it would be worth the trouble.  Just run for a few thousand steps and it should be fine. Duncan Murdoch ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.