From murdoch.duncan at gmail.com  Mon Nov  6 11:16:26 2017
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Mon, 6 Nov 2017 05:16:26 -0500
Subject: [Rd] Extreme bunching of random values from runif with
 Mersenne-Twister seed
In-Reply-To: <855A97DA-B2C5-4132-86AC-2553F8A24AA3@gmail.com>
References: <CA+S-T79BhosJQR7nQkO_Eg3pu7Kvz-0xmUOvsBR5Msa+7W+jow@mail.gmail.com>
 <CAF8bMca0D1ZVsFB40-Pqx3vquuMzUDdaETyRy9oWUm4OCrMyrg@mail.gmail.com>
 <CA+S-T7_DsigtAsj=LBxwoUWNn-2x+R_oDbvGnkDFFTgtnkcRcg@mail.gmail.com>
 <CAF8bMcb-=ijBuXmFpRC+r=ogE+y0rG3MwMvk1kM=mnYgdiWNmg@mail.gmail.com>
 <CA+S-T7_--bJ_xWh+th1KLLf3=JuzS+E6tpiJCMVe=fCGUUNYvg@mail.gmail.com>
 <cd5359ff-ecaf-b998-ef1c-b89d564b9769@gmail.com>
 <3d1b6158-d6a6-fdf1-6498-d3b0655b0113@gmail.com>
 <855A97DA-B2C5-4132-86AC-2553F8A24AA3@gmail.com>
Message-ID: <7710aca7-d936-8124-1fca-f422462abe22@gmail.com>

On 05/11/2017 10:58 AM, peter dalgaard wrote:
> 
>> On 5 Nov 2017, at 15:17 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>>
>> On 04/11/2017 10:20 PM, Daniel Nordlund wrote:
>>> Tirthankar,
>>> "random number generators" do not produce random numbers.  Any given
>>> generator produces a fixed sequence of numbers that appear to meet
>>> various tests of randomness.  By picking a seed you enter that sequence
>>> in a particular place and subsequent numbers in the sequence appear to
>>> be unrelated.  There are no guarantees that if YOU pick a SET of seeds
>>> they won't produce a set of values that are of a similar magnitude.
>>> You can likely solve your problem by following Radford Neal's advice of
>>> not using the the first number from each seed.  However, you don't need
>>> to use anything more than the second number.  So, you can modify your
>>> function as follows:
>>> function(x) {
>>>         set.seed(x, kind = "default")
>>>         y = runif(2, 17, 26)
>>>         return(y[2])
>>>       }
>>> Hope this is helpful,
>>
>> That's assuming that the chosen seeds are unrelated to the function output, which seems unlikely on the face of it.  You can certainly choose a set of seeds that give high values on the second draw just as easily as you can choose seeds that give high draws on the first draw.
>>
>> The interesting thing about this problem is that Tirthankar doesn't believe that the seed selection process is aware of the function output.  I would say that it must be, and he should be investigating how that happens if he is worried about the output, he shouldn't be worrying about R's RNG.
>>
> 
> Hmm, no. The basic issue is that RNGs are constructed so that with x_{n+1} = f(x_n),
> x_1, x_2, x_3,... will look random, not so that f(s_1), f(s_2), f(s_3), ... will look random for any s_1, s_2, ... . This is true, even if seeds s_1, s_2, ... are not chosen so as to mess with the RNG. In the present case, it seems that the seeds around 86e6 tend to give similar output. On the other hand, it is not _just_ the similarity in magnitude that does it, try e.g.
> 
> s <- as.integer(runif(1000000, 86.54e6, 86.98e6))
> r <- sapply(s, function(s){set.seed(s); runif(1,17,26)})
> plot(s,r, pch=".")
> 
> and no obvious pattern emerges. My best guess is that the seeds are not only of similar magnitude, but also have other bit-pattern similarities.
> 
> (Isn't there a Knuth quote to the effect that "Every random number generator will fail in at least one application"?)
> 
> One remaining issue is whether it is really true that the same seeds givee different output on different platforms. That shouldn't happen, I believe.

I don't think there's a platform difference if the same generator is 
used. In my tests, I get the Ubuntu results on both MacOS and Windows. 
In one of the earlier messages, Tirthankar said he was using 
RNGkind(kind = NULL), which means earlier experiments with a different 
generator would taint the results.

Duncan Murdoch



[Rd] Extreme bunching of random values from runif with Mersenne-Twister seed
Duncan Murdoch