Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #13841 > unrolled thread
| Started by | Roger Wilson <rogerhw999@gmail.com> |
|---|---|
| First post | 2013-03-14 11:13 +0000 |
| Last post | 2013-03-16 07:13 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Multiple independent random number streams cannot be implemented. Roger Wilson <rogerhw999@gmail.com> - 2013-03-14 11:13 +0000
Re: Multiple independent random number streams cannot be implemented. Vince Virgilio <blueschi@gmail.com> - 2013-03-15 05:46 +0000
Re: Multiple independent random number streams cannot be implemented. Joe Gwinn <joegwinn@comcast.net> - 2013-03-15 05:46 +0000
Re: Multiple independent random number streams cannot be implemented. Roger Wilson <rogerhw999@gmail.com> - 2013-03-16 07:13 +0000
| From | Roger Wilson <rogerhw999@gmail.com> |
|---|---|
| Date | 2013-03-14 11:13 +0000 |
| Subject | Multiple independent random number streams cannot be implemented. |
| Message-ID | <khsbbv$36f$1@smc.vnet.net> |
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]
| From | Vince Virgilio <blueschi@gmail.com> |
|---|---|
| Date | 2013-03-15 05:46 +0000 |
| Message-ID | <khucit$85$1@smc.vnet.net> |
| In reply to | #13841 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joe Gwinn <joegwinn@comcast.net> |
|---|---|
| Date | 2013-03-15 05:46 +0000 |
| Message-ID | <khucjj$8m$1@smc.vnet.net> |
| In reply to | #13841 |
In article <khsbbv$36f$1@smc.vnet.net>, 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.
SeedRandom[1] tells Mathematica to always start with a seed of 1.
The problem is that Random has internal state, so for independence you
need multiple independent sets of internal state.
So f and g need to be in different BlockRandom domains.
Joe Gwinn
[toc] | [prev] | [next] | [standalone]
| From | Roger Wilson <rogerhw999@gmail.com> |
|---|---|
| Date | 2013-03-16 07:13 +0000 |
| Message-ID | <ki163n$5um$1@smc.vnet.net> |
| In reply to | #13841 |
On Thursday, 14 March 2013 11:13:03 UTC, 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
I'm replying to myself as several people have been kind enough to post replies here and to me directly. I'll try to describe the problem in more detail. I should point out that I've worked around the issue using BlockRandom so its not longer a problem, just academic interest now.
The problem is a monte-carlo simulation involving Brownian Bridges. I have a paths which are sequences of random numbers. I start of with one number , sub-divide it into two with the Brownian bridge, sub-divide that into 4 etc. Each sub-division pulls more random numbers from the RNG. One set of paths stating with the coarsest path to the finest path (say divided into 64 steps) is one monte-carlo sample.
If I need 1000 samples I generate each the first set of sub-divided paths, then the second then third etc generating paths of lengths 1,2,4,8,16,32&64. A problem arises if I change the depth of the sub-division. Say I only sub-divided the paths into 1,2,4,8,16&32 sections not but not 64. With the same random seed, the first sample (with paths lengths 1,2,4,8,16&32) will be the same (except missing the finest layer) but the next sample will be completely different because its now consuming random variates from the RNG which would have been used to refine the finest layer of the sample before.
A solution to this would have been to use a separate RNG for each sub-division layer, saving and restoring $RandomState indexed on the layers depth. That way the random numbers used for say, layer three would be independent of the simulation having a layer 7 or not.
However $RandomState is a legacy feature and in v.9 does not change at all as the random numbers are generate (except in Legacy mode). BlockRandom is the replacement but requires a change of paradigm.
My solution has been to generate each sample of sub-divided path inside a BlockRandom with its own seed (randomly generated outside the BlockRandom). This then means that each sample will generate the same coarser paths independently of the finer paths being generated. The sequence of seeds for each sample generation can be seeded itself using SeedRandom (its outside the BlockRandom used to isolate the sample generation).
Many thanks for everyone's replies but there are too many to respond to individually.
Roger
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web