/*
   C++ class definition for use with random.cc This class implements a
   Mersenne Twister RNG, which is not only fast but has very good
   independence properties.  See the following for details:
     http://www.math.keio.ac.jp/~matumoto/emt.html

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

     1. Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

     2. Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials
        provided with the distribution.

     3. The names of its contributors may not be used to endorse or
        promote products derived from this software without specific
        prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef INCLUDED_random_h
#define INCLUDED_random_h

#include "DogTypes.h"

class Random {
  static const int N = 624;
  static const int M = 397;

  // state
  uint32_t state[N];
  uint32_t *next;
  int left;

  int gleft;
  double grand;

  void next_state();
public:
  Random() {left=-1;}

  void randomize();
  void seed(uint32_t s);
  void seed(const uint32_t *init_key,int key_length);

  // uniform random numbers
  unsigned uint32();
  int int32()
    {return((int)(uint32()));}
  int int31()
    {return((int)(uint32()>>1));}

  double real32()
    {return((double)uint32() * (1.0/4294967296.0));}
  double real53()
    {unsigned a=uint32()>>5,b=uint32()>>6;
     return(a*67108864.0+b)*(1.0/9007199254740992.0);}

  // convenience routines
  double uniform0to1(){
    return real32();
  }
  double uniformNeg1to1(){
    return -1.0 + 2.0*real32();
  }
  double uniform(double low,double high){
    return low+(high-low)*real32();
  }

  // non-uniform distributions
  double gaussian32();
};

extern Random GlobalRandom;

// generates fast approximate gaussian samples by using a pre-generated
// table with values which get replaced eventually
class FastGaussianSampler {
private:
  static const int TableSize   = 512; // size of table (must be multiple of 2)
  static const int ReplaceFreq = 4; // generate a new sample every this many queries
  double table[TableSize];
  int next_sample_idx;
  int next_replace_idx;
  int replace_countdown;
  bool initialized;

  void init();
public:
  FastGaussianSampler();
  ~FastGaussianSampler() {};

  double gen_sample()
  {
    if(!initialized){
      init();
    }
    
    if(--replace_countdown <= 0){
      replace_countdown = ReplaceFreq;
      table[next_replace_idx++] = GlobalRandom.gaussian32();
      next_replace_idx &= (TableSize-1);
    }
    
    double res=table[next_sample_idx++];
    next_sample_idx &= (TableSize-1);
    
    return res;
  }

};

extern FastGaussianSampler GlobalGaussianSampler;

#endif
