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

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

[Octave-bug-tracker] [bug #54342] rand() produces different results on o


From: Dan Sebald
Subject: [Octave-bug-tracker] [bug #54342] rand() produces different results on octave 4.4.0 compared to earlier versions
Date: Fri, 20 Jul 2018 14:16:20 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0

Follow-up Comment #3, bug #54342 (project octave):

The following seems to return the value that Inf used to:


octave:13> rand('state', NaN)
octave:14> rand
ans =  0.84442


I'm not sure what this means though, as the documentation indicates that one
should do:


octave:27> v = rand ('state');
octave:28> rand ('state', v);


Perhaps the word 'state' and 'seed' are synonymous when v above is a scalar. 
So, let's check 'seed':


octave:33> rand ('seed', Inf)
octave:34> rand
ans =  0.86444
octave:35> rand ('state', Inf)
octave:36> rand
ans =  0.63536


OK, so 'seed' is sort of like what 'state' used to do, but:


octave:39> rand ('seed', NaN)
octave:40> rand
ans =  0.92987
octave:41> rand ('state', NaN)
octave:42> rand
ans =  0.84442


So, 'seed' and 'state' are different entities.

Is there some consistency between versions for you with regard to "rand
('seed', s)"?

Just to get pointed in the right direction in the code, it seems that the
'state' option works it's way to (oct-rand.cc):


  void rand::set_internal_state (const uint32NDArray& s)
  {
    octave_idx_type len = s.numel ();

    const uint32_t *sdata = reinterpret_cast <const uint32_t *> (s.data ());

    if (len == MT_N + 1 && sdata[MT_N] <= MT_N && sdata[MT_N] > 0)
      set_mersenne_twister_state (sdata);
    else
      init_mersenne_twister (sdata, len);
  }


I've verified that


octave:2> rand ('state', NaN)
C++ std::cerr: Case 2
octave:3> rand ('state', Inf)
C++ std::cerr: Case 2
octave:4> rand ('state', [1 2])
C++ std::cerr: Case 2


all call the second function init_mersenne_twister(), whereas the full length
vector V case calls the first function set_mersenne_twister_state();


  /* initializes state[MT_N] with a seed */
  void init_mersenne_twister (const uint32_t s)
  {
    int j;
    state[0] = s & 0xffffffffUL;
std::cerr << "init_mersenne_twister uint32 value: " << s << "\n";


This is an integer value, so these floating point numbers need to be
converted.  I suspect this has something to do with that conversion from
floating point to integer.  Can anyone see what the issue is from the
following results?


octave:33> rand ('state', 1); rand
ans =  0.13436
octave:34> rand ('state', 0.5); rand
ans =  0.13436
octave:35> rand ('state', .499999); rand
ans =  0.84442
octave:36> rand ('state', -Inf); rand
ans =  0.84442
octave:37> rand ('state', -1); rand
ans =  0.84442
octave:38> rand ('state', NaN); rand
ans =  0.84442
octave:39> rand ('state', Inf); rand
ans =  0.63536


Could it be that in older versions the -Inf, -1, 0, NaN, *and* Inf all get
converted to unsigned 0, whereas now the Inf gets converted to unsigned L
where "L" means the largest unsigned 32-bit value (or something similar in
concept by which the mantissa resolution comes into play)?  Might this be a
C++11 thing?

    _______________________________________________________

Reply to this item at:

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

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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