Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13843 > unrolled thread

Re: Multiple independent random number streams cannot be implemented.

Started bySseziwa Mukasa <mukasa@gmail.com>
First post2013-03-15 05:46 +0000
Last post2013-03-19 05:51 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Multiple independent random number streams cannot be implemented. Sseziwa Mukasa <mukasa@gmail.com> - 2013-03-15 05:46 +0000
    Re: Multiple independent random number streams cannot be implemented. David Bailey <dave@removedbailey.co.uk> - 2013-03-17 23:48 +0000
      Re: Multiple independent random number streams cannot be implemented. Vince Virgilio <blueschi@gmail.com> - 2013-03-18 09:34 +0000
        Re: Multiple independent random number streams cannot be implemented. David Bailey <dave@removedbailey.co.uk> - 2013-03-19 04:05 +0000
    Re: Multiple independent random number streams cannot be implemented. Vince Virgilio <blueschi@gmail.com> - 2013-03-19 05:51 +0000

#13843 — Re: Multiple independent random number streams cannot be implemented.

FromSseziwa Mukasa <mukasa@gmail.com>
Date2013-03-15 05:46 +0000
SubjectRe: Multiple independent random number streams cannot be implemented.
Message-ID<khucj3$8c$1@smc.vnet.net>
You are asking for the opposite of independent random variables.

I can think of a couple of solutions to your problem.  If you know how 
many samples you're going to take you can initialize a vector and have f 
& g remember where they are in the vector.

HiddenRandomStateContext`fpos = 1;
HiddenRandomStateContext`gpos=1;
HiddenRandomStateContext`randomSequence = RandomReal[{0, 1}, <Maximum number of samples>];


f[]:=HiddenRandomStateContext`randomSequence[[HiddenRandomStateContext`fpos++]]

g[]:=HiddenRandomStateContext`randomSequence[[HiddenRandomStateContext`gpos++]]

I you can't  preallocate the values you have to start with the same seed and iterate:

HiddenRandomStateContext`fpos = 1;
HiddenRandomStateContext`gpos=1;
HiddenRandomStateContext`nthRandomValue[n_] = := Block[{}, SeedRandom[1]; Last[RandomReal[]]];


f[]:=HiddenRandomStateContext`nthRandomValue[HiddenRandomStateContext`fpos++]

g[]:=HiddenRandomStateContext`nthRandomValue[HiddenRandomStateContext`gpos++]

This of course scales as O(n^2) in time for the nth value.  I think you may be able to get a small speed up by memoizing the values of g[] and f[] for different numbers of calls, but I think the larger question is what are you trying to accomplish by this?

Regards,
Sseziwa

On Mar 14, 2013, at 7:14 AM, Roger Wilson <rogerhw999@gmail.com> wrote:

> Hopefully the title is enough of a red flag for someone to try to prove me wrong.
>
> I want to implement two functions; lets call them f and g so that they both return random numbers.  For example...
>
> f[]:=RandomReal[];
> g[]:=RandomReal[];
>
> However I want f and g to return the same number when they're called for the nth time.  So the first call to f is always the same number and the first call to g is always the same number.
>
> In the above case (on my machine)...
> SeedRandom[1]; {f[], g[]} gives {0.168697, 0.113119}
>
> and of course..
> SeedRandom[1]; {g[], f[]} gives the same {0.817389, 0.11142}
>
> Essentially I want f and g to to be independent random sources.
>
> It is possible to localize the random number generators using BlockRandom but I do not see how that helps me in this case.  BlockRandom localizes the state of the random number generators within the block where as I want to localize them into the symbol names.
>
> Roger
>

[toc] | [next] | [standalone]


#13867

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-03-17 23:48 +0000
Message-ID<ki5ko5$e7e$1@smc.vnet.net>
In reply to#13843
On 15/03/2013 05:46, Sseziwa Mukasa wrote:
> You are asking for the opposite of independent random variables.
>
> I can think of a couple of solutions to your problem.  If you know how
> many samples you're going to take you can initialize a vector and have f
> &  g remember where they are in the vector.
>
There is a variable called $RandomState, which is poorly documented, but 
seems (from a quick test) to hold enough information to restore the 
random number generator to any position. Thus, any process can generate 
its own random sequence:


fstate = 0;
f[] := Block[{$RandomState = $RandomState, tmp},
    If[fstate ===0,
     SeedRandom["The f function"];
     fstate = $RandomState;
     ];
    $RandomState = fstate;
    tmp = Random[];
    fstate = $RandomState;
    tmp
    ];
gstate = 0;
g[] := Block[{$RandomState = $RandomState},
   If[gstate ===0,
    SeedRandom["The g function"];
    gstate = $RandomState;
    ];
   $RandomState = gstate;
   tmp = Random[];
   gstate = $RandomState;
   tmp
   ]

Unfortunately, the documentation at 9.0 deprecates the use of 
$RandomState - stating that it is superseded by BlockRandom - but 
clearly $RandomState can solve problems which BlockRandom cannot.

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#13875

FromVince Virgilio <blueschi@gmail.com>
Date2013-03-18 09:34 +0000
Message-ID<ki6n40$gen$1@smc.vnet.net>
In reply to#13867
On Sunday, March 17, 2013 7:48:21 PM UTC-4, David Bailey wrote:
...
> Unfortunately, the documentation at 9.0 deprecates the use of 
> 
> $RandomState - stating that it is superseded by BlockRandom - but 
> 
> clearly $RandomState can solve problems which BlockRandom cannot.

Agreed. So use Random`GetRandomState[] .

Vince

[toc] | [prev] | [next] | [standalone]


#13889

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-03-19 04:05 +0000
Message-ID<ki8o5d$kon$1@smc.vnet.net>
In reply to#13875
On 18/03/2013 09:34, Vince Virgilio wrote:
> On Sunday, March 17, 2013 7:48:21 PM UTC-4, David Bailey wrote:
> ...
>> Unfortunately, the documentation at 9.0 deprecates the use of
>>
>> $RandomState - stating that it is superseded by BlockRandom - but
>>
>> clearly $RandomState can solve problems which BlockRandom cannot.
>
> Agreed. So use Random`GetRandomState[] .
>
> Vince
>
I don't understand your point, Random`GetRandomState is not documented 
at all, and $RandomState is stated to be superseded by BlockRandom!

My point is that $RandomState should not be superseded by a function 
that is less useful!

People like to use functions that are documented and not superseded, 
because that gives them confidence that they will continue to exist in 
future versions!

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#13894

FromVince Virgilio <blueschi@gmail.com>
Date2013-03-19 05:51 +0000
Message-ID<ki8ucl$o79$1@smc.vnet.net>
In reply to#13843
On Tuesday, March 19, 2013 12:05:01 AM UTC-4, David Bailey wrote:
> On 18/03/2013 09:34, Vince Virgilio wrote:
>
> > On Sunday, March 17, 2013 7:48:21 PM UTC-4, David Bailey wrote:
>
> > ...
>
> >> Unfortunately, the documentation at 9.0 deprecates the use of
>
> >>
>
> >> $RandomState - stating that it is superseded by BlockRandom - but
>
> >>
>
> >> clearly $RandomState can solve problems which BlockRandom cannot.
>
> >
>
> > Agreed. So use Random`GetRandomState[] .
>
> >
>
> > Vince
>
> >
>
> I don't understand your point, Random`GetRandomState is not documented
>
> at all, and $RandomState is stated to be superseded by BlockRandom!
>
>
>
> My point is that $RandomState should not be superseded by a function
>
> that is less useful!
>
>
>
> People like to use functions that are documented and not superseded,
>
> because that gives them confidence that they will continue to exist in
>
> future versions!
>
>
>
> David Bailey
>
> http://www.dbaileyconsultancy.co.uk

OK, not documented. But Random`GetRandomState has worked for years for me (if I recall the duration correctly). I think in versions 6-8. It's in 9 too. I forgot that Random`GetRandomState[] was not documented. And I forgot how I learned about it. Probably from WRI TS, but can't find the email.

Obviously there's a gap in the design of Mathematica's PRNG system, which you indicate. BlockRandom does not completely supersede $RandomState. Pity. I probably stumbled across this long ago, asked TS about it, and got GetRandomState back in a response. I don't think the gap can be otherwise overcome, unless I'm missing something in the documentation (tutorial/RandomNumber Generation, around section "Defining Your Own Generator").

WRI should document and baseline it in the next release (major, minor, or point), hm?


Vince

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web