From: carl on 14 Apr 2010 14:16 I am trying to generate 100 random numbers (integer) from 1 to 10. Currently I do: 1 + round(rand(100,1)*9) but is that the correct/optimal approach?
From: Jos (10584) on 14 Apr 2010 14:32 "carl" <carl@.com> wrote in message <4bc6067b$0$285$14726298(a)news.sunsite.dk>... > I am trying to generate 100 random numbers (integer) from 1 to 10. Currently > I do: > > 1 + round(rand(100,1)*9) > > but is that the correct/optimal approach? This *not* the correct approach. See for yourself: N = 10000 ; R = 1 + round(rand(N,1)*9) ; histc(R,1:10) % not the desired distribution I expect here is one option N = 10000 ; R = ceil(rand(N,1)*10) ; histc(R,1:10) Another option is using RANDI hth Jos
From: Jan Simon on 14 Apr 2010 17:14 Dear Jos! > R = ceil(rand(N,1)*10) ; I'm still confused by: http://mindprod.com/jgloss/pseudorandom.html There you find: >>>>>>>>>>>>>>>>>>>> nextDouble() works by taking 53 random bits divided by (double) (1 << 53). So, the closest it can get to 1.0 is 0.99999999999999988897. When you multiply by a sufficiently large number, the tiny difference from 1.0 gets lost, and the result is the same as if you had started with 1.0 not a number just less than 1.0. According to Merlin Hughes, any number that the generator produces will occur 32 times more commonly than for a perfect distribution; 31 of every 32 numbers just won’t ever be produced. About every four days someone will post the following code as a way to generate a random integer 0 .. 10: int m = (int)( Math.random() * 10.0d ); // not recommended!! .... 2. However the technique in general may once in a blue moon generate "10". It won’t actually do this with 10, but it will hit the upper bound with larger ranges, so I think it wise a avoid the technique on general principles. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< We do not discuss about Java's Math.random here, but Matlab's RAND use the same definition (does it?): Jan
From: ade77 on 14 Apr 2010 18:27 "carl" <carl@.com> wrote in message <4bc6067b$0$285$14726298(a)news.sunsite.dk>... > I am trying to generate 100 random numbers (integer) from 1 to 10. Currently > I do: > > 1 + round(rand(100,1)*9) > > but is that the correct/optimal approach? If you just want uniformly distributed integers from 1 to 10, you can use 'randi'. data = randi(10,100,1). % integer from 1 to 10, 100 rows, 1 column.
From: Bruno Luong on 14 Apr 2010 23:44 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hq5b6r$bju$1(a)fred.mathworks.com>... > Dear Jos! > > > R = ceil(rand(N,1)*10) ; > > I'm still confused by: > http://mindprod.com/jgloss/pseudorandom.html > > There you find: >>>>>>>>>>>>>>>>>>>> > nextDouble() works by taking 53 random bits divided by (double) (1 << 53). So, the closest it can get to 1.0 is 0.99999999999999988897. When you multiply by a sufficiently large number, the tiny difference from 1.0 gets lost, and the result is the same as if you had started with 1.0 not a number just less than 1.0. According to Merlin Hughes, any number that the generator produces will occur 32 times more commonly than for a perfect distribution; 31 of every 32 numbers just won’t ever be produced. > > About every four days someone will post the following code as a way to generate a random integer 0 .. 10: > > int m = (int)( Math.random() * 10.0d ); // not recommended!! > ... > 2. However the technique in general may once in a blue moon generate "10". It won’t actually do this with 10, but it will hit the upper bound with larger ranges, so I think it wise a avoid the technique on general principles. > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< > > We do not discuss about Java's Math.random here, but Matlab's RAND use the same definition (does it?): If I correctly understand your concern Jan, I believe the round-off side effect described above does NOT happens with CEIL (but with FLOOR). Thus you see many posts in this newsgroup using CEIL to generate random integers. The use of CEIL is not only to cope with Matlab 1-based indexing, but also for the very reason you stated. Bruno
|
Next
|
Last
Pages: 1 2 Prev: Executing Matlab code using Visual basic Next: Weird Mex behavior in RTW |