关于随机数生成的一些代码(C/C++)
2009-07-24 14:17
363 查看
一、/////////////////////////////////////////////////////////////////////////////
// ELEMENT GDI/DX Game Engine [Version 2002/3/1]
/////////////////////////////////////////////////////////////////////////////
// Original Author : 邱海峰[Southdy]
// OICQ : 359766
// EMAIL: topmud@263.net
/////////////////////////////////////////////////////////////////////////////
// DESCRIPTION :
//
// OTHER : 邱海峰创建于2002/3/1
//
// 使用此随机数生成器者请无条件保留以下版权声明:
// (C) Copyright Beman Dawes 1998. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
/////////////////////////////////////////////////////////////////////////////
#pragma once
/////////////////////////////////////////////////////////////////////////////
// 包含文件
/////////////////////////////////////////////////////////////////////////////
#include <cassert>
/////////////////////////////////////////////////////////////////////////////
class EK_Rand
{
// 不要有改动这几个常量的想法,否则……打你屁屁!
enum
{
modulus = 2147483647L,
multiplier = 48271L,
validation = 399268537L,
q = modulus / multiplier,
r = modulus % multiplier
};
// 种子数
long value; // 0 < value <= modulus
public:
explicit min_rand( long seed_value=1 ) : value( seed_value )
{
assert( value > 0 && value <= modulus );
}
operator long() const { return value; }
double fvalue() const { return double(value) / modulus; }
min_rand& operator=( long new_value )
{
value = new_value;
assert( value > 0 && value <= modulus );
return *this;
}
long operator++()
{
value = multiplier*(value%q) - r*(value/q);
if ( value <= 0 ) value += modulus;
assert( value > 0 && value <= modulus );
return value;
}
long operator++(int) { long temp = value; operator++(); return temp; }
long ten_thousandth() const { return validation; }
long operator()( long n ) { return operator++() % n; }
long operator()() { return operator++(); }
typedef long argument_type;
typedef long result_type;
};
/////////////////////////////////////////////////////////////////////////////
在boost里看到的绝对好东东。
在保留原作者版权声明的条件下可以随意使用 :)
二、
#include <iostream>
#include <ctime>
using namespace std;
class Rand
{
unsigned int seed;
public:
void srand(unsigned int seed){seed = seed;};
unsigned int rand(){seed = seed*1103515245+12345; return (seed>>16)%32768;}
};
int main(int argc, char* argv[])
{
Rand rd;
rd.srand(time(NULL));
for (int i = 0; i < 255; ++i)
cout << rd.rand() << '/t';
return 0;
}
// ELEMENT GDI/DX Game Engine [Version 2002/3/1]
/////////////////////////////////////////////////////////////////////////////
// Original Author : 邱海峰[Southdy]
// OICQ : 359766
// EMAIL: topmud@263.net
/////////////////////////////////////////////////////////////////////////////
// DESCRIPTION :
//
// OTHER : 邱海峰创建于2002/3/1
//
// 使用此随机数生成器者请无条件保留以下版权声明:
// (C) Copyright Beman Dawes 1998. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
/////////////////////////////////////////////////////////////////////////////
#pragma once
/////////////////////////////////////////////////////////////////////////////
// 包含文件
/////////////////////////////////////////////////////////////////////////////
#include <cassert>
/////////////////////////////////////////////////////////////////////////////
class EK_Rand
{
// 不要有改动这几个常量的想法,否则……打你屁屁!
enum
{
modulus = 2147483647L,
multiplier = 48271L,
validation = 399268537L,
q = modulus / multiplier,
r = modulus % multiplier
};
// 种子数
long value; // 0 < value <= modulus
public:
explicit min_rand( long seed_value=1 ) : value( seed_value )
{
assert( value > 0 && value <= modulus );
}
operator long() const { return value; }
double fvalue() const { return double(value) / modulus; }
min_rand& operator=( long new_value )
{
value = new_value;
assert( value > 0 && value <= modulus );
return *this;
}
long operator++()
{
value = multiplier*(value%q) - r*(value/q);
if ( value <= 0 ) value += modulus;
assert( value > 0 && value <= modulus );
return value;
}
long operator++(int) { long temp = value; operator++(); return temp; }
long ten_thousandth() const { return validation; }
long operator()( long n ) { return operator++() % n; }
long operator()() { return operator++(); }
typedef long argument_type;
typedef long result_type;
};
/////////////////////////////////////////////////////////////////////////////
在boost里看到的绝对好东东。
在保留原作者版权声明的条件下可以随意使用 :)
二、
#include <iostream>
#include <ctime>
using namespace std;
class Rand
{
unsigned int seed;
public:
void srand(unsigned int seed){seed = seed;};
unsigned int rand(){seed = seed*1103515245+12345; return (seed>>16)%32768;}
};
int main(int argc, char* argv[])
{
Rand rd;
rd.srand(time(NULL));
for (int i = 0; i < 255; ++i)
cout << rd.rand() << '/t';
return 0;
}
相关文章推荐
- 关于Excel Vba 自动生成代码的一些研究
- 一些设计图--关于代码生成
- 关于android 代码生成布局中遇到的一些问题
- 关于java常用类Math的一些用法:计算数的绝对值,两个值比较大小,取0~1之间随机数
- 《关于SQL语句的自动生成!》一文的代码调用示例
- Android2.2源代码中关于生成图片缩略图的一个bug
- win7安装python生成随机数代码分享
- 关于SubSonic3.0生成的表名自动加复数(s)的“用户代码未处理SqlException,对象名'xxxs'无效”异常处理
- 关于字符串指针的一些问题及字符串的左旋转代码
- LLVM每日谈之二十一 一些关于编译器和LLVM/Clang的代码
- 关于zeo作品中生成代码使用方法的说明!
- 自己写的一些代码关于setInterval和setTimeout
- 关于时间转化的一些代码
- 关于代码生成的一点想法
- 代码编写中关于通用定义的一些个性规约
- 算法:关于生成抽样随机数的这些算法
- 关于代码一些优化
- python 随机数生成的代码的详细分析
- 关于myeclipse代码提示的一些问题
- 关于开源中国代码托管的一些问题