Prev: Speed bottleneck
Next: Error message for rayleighchan
From: someone on 22 Jul 2010 12:26 "Paulo " <paulojmdsilva(a)gmail.com> wrote in message <i29le8$2pa$1(a)fred.mathworks.com>... > "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i29gvb$cs2$1(a)fred.mathworks.com>... > > "Paulo " <paulojmdsilva(a)gmail.com> wrote in message <i297dj$bd7$1(a)fred.mathworks.com>... > > > "jay " <ssjzdl(a)gmail.com> wrote in message <i22vhv$frn$1(a)fred.mathworks.com>... > > > > I need to generate 5 random variables between [0,1], let's say a, b, c, d, e. The constraint is that a<b<c<d<e. How to make this happen? Please advise. thanks > > > > > > > > > while(1) %infinite loop > > > a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values on variables > > > if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if constraint is true > > > %if constraint is true than stop the loop > > > end > > > fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values > > > > This is a rejection method, quite an inefficient way to > > do the same thing as a sort on this problem. Since > > there are 5! = 24 ways to generate a set of 5 numbers, > > only ONE of which is sorted, this dumps its results into > > the bit bucket nearly 96% of the time. > > > > 23/24 > > ans = > > 0.958333333333333 > > > > It makes far more sense to generate 5 numbers (as a > > vector, in ONE operation) and then use sort on them. > > > > sort(rand(1,5)) > > > > The statistics of this result are the same in the end, yet > > it wastes far less cpu time to do the operation. > > > > John > > That's a good point but unless he wants to create those variables many many times it doesn't matter much, This is a little out of my expertise, but it COULD matter. I believe your code is an example of "indefinite postponement". It is possible you might go through your while loop an infinite number of times before you finally find a solution that breaks out of the loop. When John said (correct me if I'm wrong) "96% of the time" I believe that is an average number. There is no guarantee. > also there's no problem with variables when they got the same value, my code is simple and doesn't require any fancy manipulations or functions. Do you really think the while loop is simplier than the one liner solution?
From: Walter Roberson on 22 Jul 2010 14:43
someone wrote: > "Paulo " <paulojmdsilva(a)gmail.com> wrote in message > <i29le8$2pa$1(a)fred.mathworks.com>... >> > > > > > > while(1) %infinite loop >> > > a=rand();b=rand();c=rand();d=rand();e=rand(); %put random values >> on variables >> > > if((a<b) && (b<c) && (c<d) && (d<e)) , break ,end %test if >> constraint is true >> > > %if constraint is true than stop the loop >> > > end >> > > fprintf('a=%d b=%d c=%d d=%d',a,b,c,d); % just to show the values > I believe > your code is an example of "indefinite postponement". It is possible > you might go through your while loop an infinite number of times before > you finally find a solution that breaks out of the loop. > When John said (correct me if I'm wrong) "96% of the time" I believe > that is an average number. There is no guarantee. This is a situation of a geometric distribution with probability of success p = 1/24 . The "expected value" of such a distribution is success on the 1/p'th trial (that is, the 24th trial in this case), but Yes, it does have an infinite tail. An infinite tail will occur for all rejection methods whose trials are independent and of finite non-unitary probability. The sort method would succeed in any situation in which no two random values were the same. The probability of identical random values within N trials depends substantially upon the details of the psuedo-random number generator. The old linear congruential generator could not produce duplicate numbers in less than 2^32 trials. I do not know how to analyze the subtract-with-borrow generator. The Mersenne Twister generator has been analyzed to be statistically independent up to 633 dimensions (I think the number is); I calculate that the probability of failure for it would be 99035202923775336283058995157/42535295785889145473997720539375861762 which is about 2.3E-9, leading to an average number of trials of 1 + 2.3E-9 |