octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #53299] Several rand/randn bugs with state/see


From: Dave Goel
Subject: [Octave-bug-tracker] [bug #53299] Several rand/randn bugs with state/seed behavior.
Date: Wed, 7 Mar 2018 01:57:47 -0500 (EST)
User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36

URL:
  <http://savannah.gnu.org/bugs/?53299>

                 Summary: Several rand/randn bugs with state/seed behavior.
                 Project: GNU Octave
            Submitted by: deego
            Submitted on: Wed 07 Mar 2018 06:57:45 AM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Incorrect Result
                  Status: None
             Assigned to: None
         Originator Name: DAVE GOEL
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 4.2.1
        Operating System: GNU/Linux

    _______________________________________________________

Details:

version ## =>  4.2.1

## BUG 1: rand and randn have independent seeds, yet resetting rand's seed
resets that of randn. If not a bug, this is at least undocumented behavior.
## Curiously it doesn't work the other way. 

aa = sum(rand('state')); randn('state', 'reset'); bb = sum(rand('state'));
assert(aa==bb);

aa = sum(randn('state')); rand('state', 'reset'); bb = sum(randn('state'));
assert(aa==bb);


aa = sum(rand('seed')); randn('seed', 'reset'); bb = sum(rand('seed'));
assert(aa!=bb); 
## ^^^ Bug 1a: Unexpected seed change for rand upon resetting randn.

aa = sum(randn('seed')); rand('seed', 'reset'); bb = sum(randn('seed'));
assert(aa==bb); 
## ^^^ But, resetting rand does NOT reset randn's seed! 

## Bug 1 is not even self-consistent. Resetting randn's seed resets that of
rand, but not the other way. Furthermore, resetting state of one does not
affect that of another. 


----


## BUG 2: Switching rand to use seed switches randn's behavior as well, and
vice versa!
## Again, if a bug, it is undocumented behavior.

## Now, we can't ask rand or randn to report if they are using seed or state,
so we will verify this indirectly: 


## First ensure we are using state. 
randn('state', 'reset'); 
rand('state', 'reset');

## Sanity checks and warmup: 

## These variables keep track of the state. 
nt = sum(randn('state')) ## n for randn and t for state =>nt
rt = sum(rand('state'))
nd = randn("seed")    ## n for randn and d for seed => nd
rd = rand("seed")

## We see that all 4 are different above. 

## Now, let's verify that drawing a randn changes the randn state, but NOT the
other 3.
randn(1); 
assert(nt != sum(randn('state')));
assert(rt == sum(rand('state')));
assert(nd == sum(randn('seed')));
assert(rd == sum(rand('seed')));


nt = sum(randn('state')); ## Set nt back to the current randn state. 

## Similarly, drawing a rand changes the rand state but not the other 3 

rand(1); 
assert(nt == sum(randn('state')));
assert(rt != sum(rand('state'))); 
assert(nd == sum(randn('seed')));
assert(rd == sum(rand('seed')));

rt = sum(rand("state")); ## update since we drew a rand.

## Now, switch randn to seed, and draw a randn.

randn('seed','reset');  
nd = randn('seed');  ## update since we had reset above. 
rd = rand('seed'); ## This update is needed because of Bug 1 above. rand seed
has also changed. 
randn(1); ## 

## Verify that nd has changed, but not others. 
assert(nt == sum(randn('state')));
assert(rt == sum(rand('state'))); 
assert(nd != sum(randn('seed')));
assert(rd == sum(rand('seed')));

nd = sum(randn('seed')); ## Update nd since state changed.

## We'd expect rand to still use state, but it seems that rand has switched to
using seed as well! 
rand(1);  ## draw a rand. You'd expect rt to change, but it is rd that
changes! (((bug 2a)))

## Verify that rd has changed, but not the others 
assert(nt == sum(randn('state')));
assert(rt == sum(rand('state'))); 
assert(nd == sum(randn('seed')));
assert(rd != sum(rand('seed')));

rd = sum(rand('seed'));

## Now, switch rand back to state, and update the corresponding variable
rand('state' ,'reset'); rt = sum(rand('state'));

## For things to be somewhat consistent with bug 2a, we expect randn to switch
back to state as well. Verify:
randn(1); ## draw a randn.


## Verify (((bug 2b))) that nt has changed, but not 
assert(nt != sum(randn('state')));
assert(rt == sum(rand('state'))); 
assert(nd == sum(randn('seed')));
assert(rd == sum(rand('seed')));

## We successfully reach here. 
 






    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?53299>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]