Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Multiple independent random number streams cannot be implemented.
Replies: 3   Last Post: Mar 16, 2013 3:13 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Vince Virgilio

Posts: 135
Registered: 4/30/05
Re: Multiple independent random number streams cannot be implemented.
Posted: Mar 15, 2013 1:46 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

It is possible, but I found it requires a couple of layers of abstraction.
(Perhaps there's an easier way.)

Basically, wrap calls to RandomReal[] etc. with something that can hold persistent state. On each call, SeedRandom[lastState], then lastState = Random`GetRandomState[]. The body looks something like this:

method[Hold[e__] ] := BlockRandom[SeedRandom@lastState;
With[{result = e}, lastState = Random`GetRandomState[]; result]];

(Implementation of <object>`method not shown. It uses my own flyweight OO framework.)

This has allowed me to run an arbitrary number of unique and reproducible random # streams.

Sorry that there's no time for more detail now.

Vince

On Thursday, March 14, 2013 7:13:03 AM UTC-4, 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






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.