Message Boards Message Boards

0
|
2958 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Repeatable results with random number generation?

Not getting reproducible results from the following code:

In[1]:= genRands[{nRands_, randSeed_, randMethod_}, 
  lbubs_?MatrixQ] :=
  Block[{n, s, t, r, randMethodUsed},
  n = Length[lbubs];
  s = Table[{0, 1}, n];
  t = RescalingTransform[s, lbubs];
  randMethodUsed = If[randMethod == "", "ExtendedCA", randMethod];

  r = BlockRandom[SeedRandom[randSeed];
    SeedRandom[Method -> randMethodUsed];
    RandomReal[1, {nRands, n}]];

  t /@ r]

In[2]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[2]= {{0.230599, 1.45249}, {0.88902, 1.23013}, {0.301819, 
  1.0984}, {0.678615, 1.90594}, {0.38124, 1.78955}}

In[3]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[3]= {{0.500758, 1.29848}, {0.764523, 1.62307}, {0.70913, 
  1.08896}, {0.143426, 1.96068}, {0.212094, 1.33145}}
POSTED BY: Frank Kampas
5 Replies

Your method works in one notebook session, but I get different results in different sessions.

In[1]:= genRands[{nRands_, randSeed_, randMethod_}, lbubs_?MatrixQ] := 
 Block[{n, s, t, r, randMethodUsed}, n = Length[lbubs];
  s = Table[{0, 1}, n];
  t = RescalingTransform[s, lbubs];
  randMethodUsed = If[randMethod == "", "ExtendedCA", randMethod];
  SeedRandom[Method -> randMethodUsed];
  r = BlockRandom[SeedRandom[Method -> randMethodUsed];
    RandomReal[1, {nRands, n}], RandomSeeding -> randSeed];
  t /@ r]

In[2]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[2]= {{0.658186, 1.33685}, {0.750221, 1.05406}, {0.554339, 1.60156}, {0.333813, 
  1.1567}, {0.306864, 1.94476}}

 close notebook and restart genRands

In[2]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[2]= {{0.549267, 1.18874}, {0.813163, 1.91422}, {0.564126, 1.3203}, {0.337698, 
  1.46146}, {0.568895, 1.14699}}
POSTED BY: Frank Kampas

Interesting. It appears that for "Congruential" method, the first call on a new Kernel gives a different answer but all subsequent calls work properly. It works fine for ExtendedCA. I would report this as a bug. This code fixes it -- I set the method before the BlockRandom[] and also inside -- you must do it inside or it gets ignored. The bug must be that the first call inside BlockRandom uses the old method until the new method gets set.

genRands[{nRands_, randSeed_, randMethod_}, lbubs_?MatrixQ] := 
  Block[{n, s, t, r, randMethodUsed}, n = Length[lbubs];
    s = Table[{0, 1}, n];
    t = RescalingTransform[s, lbubs];
    randMethodUsed = If[randMethod == "", "ExtendedCA", randMethod]; 
  SeedRandom[Method -> randMethodUsed];
    r = BlockRandom[SeedRandom[Method -> randMethodUsed];
        RandomReal[1, {nRands, n}], RandomSeeding -> randSeed];
    t /@ r]

Regards,

Neil

POSTED BY: Neil Singer

Thank you

POSTED BY: Frank Kampas

Your method doesn't work on my computer

In[1]:= genRands[{nRands_, randSeed_, randMethod_}, lbubs_?MatrixQ] :=
Block[{n, s, t, r, randMethodUsed}, n = Length[lbubs];
s = Table[{0, 1}, n];
t = RescalingTransform[s, lbubs];
randMethodUsed = If[randMethod == "", "ExtendedCA", randMethod];
r = BlockRandom[SeedRandom[Method -> randMethodUsed];
RandomReal[1, {nRands, n}], RandomSeeding -> randSeed];
t /@ r]

In[2]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[2]= {{0.954939, 1.95539}, {0.123899, 1.72834}, {0.562609, 
1.41635}, {0.577752, 1.08028}, {0.405537, 1.27338}}

In[3]:= genRands[{5, 0, "Congruential"}, {{0, 1}, {1, 2}}]

Out[3]= {{0.480004, 1.75959}, {0.35327, 1.52677}, {0.693529, 
1.09362}, {0.550063, 1.76284}, {0.259877, 1.26783}}
POSTED BY: Frank Kampas

Frank,

BlockRandom has its own local seed. This does produce repeatable results for the same seed:

genRands[{nRands_, randSeed_, randMethod_}, lbubs_?MatrixQ] := 
 Block[{n, s, t, r, randMethodUsed}, n = Length[lbubs];
  s = Table[{0, 1}, n];
  t = RescalingTransform[s, lbubs];
  randMethodUsed = If[randMethod == "", "ExtendedCA", randMethod];
  r = BlockRandom[
    SeedRandom[Method -> randMethodUsed];
    RandomReal[1, {nRands, n}], RandomSeeding -> randSeed];
  t /@ r]

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract