From: Jan Simon on 15 Apr 2010 05:46 Dear Bruno! > 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. Thanks! I'll try to test this with random numbers using less than 53 bits, so it should be easier to detect a bias. Jan
From: Peter Perkins on 15 Apr 2010 16:42 On 4/14/2010 5:14 PM, Jan Simon wrote: > 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. > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Jan, I may just be misunderstanding what these statements say, but as I read them, they just don't hold water. The largest number that RAND could return is 1-eps/2, and multiplying that by 10 gives you something less than 10: >> floor((1-eps/2)*10) ans = 9 In any case, just use RANDI.
First
|
Prev
|
Pages: 1 2 Prev: Executing Matlab code using Visual basic Next: Weird Mex behavior in RTW |