Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Multiple independent random number streams cannot be implemented.
Replies: 6   Last Post: Mar 19, 2013 1:51 AM

 Messages: [ Previous | Next ]
 Sseziwa Mukasa Posts: 108 Registered: 8/26/07
Re: Multiple independent random number streams cannot be implemented.
Posted: Mar 15, 2013 1:46 AM

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
>

Date Subject Author
3/15/13 Sseziwa Mukasa
3/17/13 David Bailey
3/18/13 Vince Virgilio
3/19/13 David Bailey
3/19/13 Vince Virgilio
3/16/13 waku