码迷,mamicode.com
首页 > 其他好文 > 详细

干扰数

时间:2015-02-06 18:51:16      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:


开发环境VS2005(VC8)

#include <iostream>
#include <vector>
using namespace std ;
#include <time.h>

#define ULONG unsigned long

//干扰数的意义
//当要连续调用rand的时候,几个rand关联性太强,可以rand()+干扰数
class IGanRaoShu //干扰数,取一个数,分布没有任何规律,可以重复。配合随机数,防止随机数规律过强。
{
public:
    virtual ULONG GetGanRao()= 0;
};

class CGanRaoShu : public IGanRaoShu
{
public:
    typedef long (* GANRAO_FUN)() ;

    CGanRaoShu()
    {
        m_iPos = 0 ;
    };
    ULONG GetGanRao()
    {
        if( 0 == m_pFuns.size() )
            return 0;
        m_iPos = ( m_iPos + 1 ) % m_pFuns.size();
        return m_pFuns[m_iPos]();
    };
    void AddGanRaoFun(GANRAO_FUN pFun)
    {
        m_pFuns.push_back(pFun);
    };    
protected:
    std::vector<GANRAO_FUN> m_pFuns;
    int m_iPos ;
};

long GetCurTime()
{
    return time(NULL);
}

void main()
{
    CGanRaoShu g ;
    g.AddGanRaoFun(clock);
    g.AddGanRaoFun(GetCurTime);
    for( int i = 0 ; i < 100 ; i++ )
    {
        cout << g.GetGanRao() << endl;
    }    
}

干扰数

标签:

原文地址:http://blog.csdn.net/he_zhidan/article/details/43567447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!