
/****************************************************************

  File name   :  ArnoldRandom.h
  Author      :  叶飞影
  Version     :  2.0
  Create Date :  2014/03/06   
  Description :  随机数生成器
  Others      :  

*****************************************************************/

#ifndef __ArnoldRandom_h__
#define __ArnoldRandom_h__

// --------------------------------------------------------------------------------------

class ArnoldRandom
{
public:
    ArnoldRandom()
    {
        m_seed_x = 0.0f;
        m_seed_y = 0.0f;
        m_x = m_seed_x;
        m_y = m_seed_y;
        m_iteration = 0;
    }

    // 设置种子点
    void SetSeed(float x, float y)
    {
        m_seed_x = x;
        if (m_seed_x < 0.0f)
        {
            m_seed_x = -m_seed_x;
        }
        while(m_seed_x > 1.0f)
        {
            m_seed_x -= 1.0f;
        }

        m_seed_y = y;
        if (m_seed_y < 0.0f)
        {
            m_seed_y = -m_seed_y;
        }
        while(m_seed_y > 1.0f)
        {
            m_seed_y -= 1.0f;
        }

        m_x = m_seed_x;
        m_y = m_seed_y;
        m_iteration = 0;
    }

    // 重置种子点
    void ResetSeed()
    {
        m_x = m_seed_x;
        m_y = m_seed_y;
        m_iteration = 0;
    }

    // 返回一个0到1的浮点数
    float GetRandom()
    {
        if (m_iteration & 1)
        {
            m_y = m_x + m_y*2.0f + 0.00001f;
            if (m_y > 2.0f)
            {
                m_y -= 2.0f;
            }
            else if (m_y > 1.0f)
            {
                m_y -= 1.0f;
            }
            m_x = m_current;
            m_current = m_y;
        }
        else
        {
            m_current = m_x + m_y;
            if (m_current > 1.0f)
            {
                m_current -= 1.0f;
            }
        }

        ++m_iteration;
        return m_current;
    }

    float GetRandom(float _value)
    {
        return GetRandom() * _value;
    }

    float GetRandom(float _min, float _max)
    {
        return _min + GetRandom() * (_max - _min);
    }

private:
    float m_seed_x, m_seed_y;
    float m_x, m_y;
    float m_current;
    int m_iteration;
};

// --------------------------------------------------------------------------------------

#endif