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


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

Re: Multiple independent random number streams cannot

Started byWaclaw Kusnierczyk <waku@idi.ntnu.no>
First post2013-03-16 07:13 +0000
Last post2013-03-16 07:13 +0000
Articles 1 — 1 participant

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 Waclaw Kusnierczyk <waku@idi.ntnu.no> - 2013-03-16 07:13 +0000

#13858 — Re: Multiple independent random number streams cannot

FromWaclaw Kusnierczyk <waku@idi.ntnu.no>
Date2013-03-16 07:13 +0000
SubjectRe: Multiple independent random number streams cannot
Message-ID<ki162s$5u5$1@smc.vnet.net>
You can simulate some sort of behavior that you seem to want using 
random blocks nested within modules where the internal state of the 
random generator is explicitly preserved.  For example, the following 
does the trick (make efforts to improve if you like the idea):

Generator[init_: 1, generate_: RandomReal] :=
  Module[{r, out},
   BlockRandom[
    SeedRandom[init];
    r = $RandomState;
    BlockRandom[
      SeedRandom[r];
      out = generate[];
      r = $RandomState;
      out] &]]

The idea here is to a) locally initalize the generator with the 
specified (or default) value; b) preserve the current internal state of 
the rng in a closure; c) get hold of a function that will iteratively 
use the value to set the state of the generator, generate a random 
number, and then capture the new state in the local variable.

For example,

(* two independent generators that should produce the same sequences of 
numbers *)
{f, g} = Table[Generator[], {2}]

(* compare two separately generated 5-element sequences *)
Table[f[], {5}] == Table[g[], {5}]

(* compare five pairs of values generated in parallel *)
And @@ (Equal /@ Table[Through[{f, g}[]], {5}])

vQ





On 03/14/2013 12:14 PM, Roger Wilson 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] | [standalone]


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


csiph-web