Skip to main content
13 events
when toggle format what by license comment
May 9, 2012 at 17:03 comment added Macro I've deleted the R code examples, as I think it takes away from the question of describing a conceptual algorithm to generate discrete random variables and makes it more of an R coding answer.
May 9, 2012 at 17:01 history edited Macro CC BY-SA 3.0
deleted R code example, as I didn't feel it was relevant, since this was meant to describe a conceptual algorithm.
May 9, 2012 at 16:41 history edited Macro CC BY-SA 3.0
deleted 113 characters in body
May 9, 2012 at 16:33 comment added whuber OK, that is an algorithm. BTW, why don't you just return something like min(which(u < cp))? It would be good to avoid recomputing the cumulative sum on each call, too. With that precomputed, the entire algorithm is reduced to min(which(runif(1) < cp)). Or better, because the OP asks to generate numbers (plural), vectorize it as n<-10; apply(matrix(runif(n),1), 2, function(u) min(which(u < cp))).
May 9, 2012 at 16:13 comment added Macro Fair enough, @whuber, see my edit.
May 9, 2012 at 16:13 history edited Macro CC BY-SA 3.0
added 395 characters in body
May 9, 2012 at 16:04 comment added whuber But it's only a characterization and not an effective algorithm: it still needs to specify how to go about finding $j$ in step (2). Much rests on this. E.g., the reply by @Greg Snow provides such an algorithm, albeit a terribly inefficient one. My comment to the original question provides a more efficient algorithm, good enough for many purposes, but not the best.
May 9, 2012 at 16:01 comment added Macro @whuber, I posted this after noticing the original poster said "Personally, i would prefer an algorithm (or somewhere to learn the necessary knowledge) since I am trying to incorporate this into an app that I am building" in response to the top voted answer. This answer is describing an algorithm, conceptually.
May 9, 2012 at 16:00 comment added whuber This reply, like the similar one by @David M Kaplan, sidesteps the real issue: coding step (2) to test for membership in each of the $m$ intervals.
Apr 21, 2012 at 16:33 comment added jbowman On a finite-precision digital machine, though, maybe someday before the end of the universe it will matter...
Apr 21, 2012 at 3:58 comment added Macro $P(U=u)=0$ for any point $u$ (i.e. the Lebesgue measure of the half open interval is the same as that of the open interval) so I don't think it matters.
Apr 21, 2012 at 0:53 comment added naught101 Shouldn't those intervals all be half-closed? Otherwise the boundaries between intervals are not included.. ie. $\{[0,0.04),\ [0.04,0.54),\ [0.54,1]\}$
Apr 21, 2012 at 0:05 history answered Macro CC BY-SA 3.0