Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.8K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

Repeatable results with random number generation?

POSTED BY: Frank Kampas
5 Replies

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

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

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