随机数码 [英] random number code

查看:73
本文介绍了随机数码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要一种在c ++中使用随机数的方法。在我的c ++项目中,当使用

mingw编译器时,我使用了一个公开可用的mersenne twister和

这很好地完成了它的工作。现在我已经开始对VC ++ 6.0进行编译了,因为相同的代码证明是困难的。我不太担心如何在c ++中生成随机的

数字,只要它足够随机。


任何人都可以帮我解决问题想要VC ++?


如果有人想要现有代码以及VC ++生成的错误那么

见下文。也许有人可以解释问题是什么,以及如何修复

。对不起,这很长。


在一个单独的点上,正如你可能认为的那样,购买

的东西是非常令人失望的,它承诺能够编译ISO C ++然后从之前编译好的代码中抛出

错误。关于这段代码,我不是在谈论

,因为我面临用VC ++重写整个项目以适应

。我在想这是因为我没有在VC ++中用

创建类,所以也许我可以将代码复制并粘贴到这个该死的东西中。

部分 - 任何对此有何建议?我希望VC ++能够使用mfc并简单地创建一个

GUI,并利用其调试工具和

代码提示。它真的编译与在DevC ++中使用的mingw编译器完全相同吗?或者我是否需要获得一本好的VC ++书籍,因为它不同?

有关什么的任何建议阅读以充分利用它?


代码:


// mtrand.cpp,有关信息,请参阅包含文件mtrand.h br />

#include" mtrand.h"

//非内联函数定义和静态成员定义不能

//驻留在头文件中因为多次声明的风险


//静态私有成员的初始化

unsigned long MTRand_int32 :: state [n] = {0x0UL} ;

int MTRand_int32 :: p = 0;

bool MTRand_int32 :: init = false;


void MTRand_int32 :: gen_state (){//生成新的状态向量

for(int i = 0; i<(n - m); ++ i)

state [i] = state [i + m] ^ twiddle(state [i],state [i + 1]);

for(int i = n - m; i<(n - 1); ++ i)

州[i] = state [i + m - n] ^ twiddle(state [i],state [i + 1]);

state [n - 1] = state [m - 1] ^ twiddle(state [n - 1],州[0]);

p = 0; //重置位置

}


void MTRand_int32 :: seed(unsigned long s){//由32位种子初始化

state [0] = s& 0xFFFFFFFFUL; // for> 32位机器

for(int i = 1; i< n; ++ i){

state [i] = 1812433253UL *(state [i - 1] ^(state [i - 1]>> 30))+ i;

//参见Knuth TAOCP Vol2。第三版。 P.106乘法器

//在以前的版本中,种子的MSB只影响阵列的MSB

state

// 2002 / 01/09由Makoto Matsumoto修改

state [i]& = 0xFFFFFFFFUL; // for> 32位机器

}

p = n; //强制gen_state()调用下一个随机数

}


void MTRand_int32 :: seed(const unsigned long * array,int size){ // init by

array

seed(19650218UL);

int i = 1,j = 0;

for(int k =((n> size)?n:size); k; --k){

state [i] =(state [i] ^((state [i - 1] ^(州[i - 1]>> 30))*

1664525UL))

+ array [j] + j; //非线性

state [i]& = 0xFFFFFFFFUL; // for> 32位机器

++ j; j%= size;

if((++ i)== n){state [0] = state [n - 1]; i = 1; }

}

for(int k = n - 1; k; --k){

state [i] =(state [ i] ^((state [i - 1] ^(state [i - 1]>> 30))*

1566083941UL)) - i;

state [i]& = 0xFFFFFFFFUL; // for> 32位机器

if((++ i)== n){state [0] = state [n - 1]; i = 1; }

}

state [0] = 0x80000000UL; // MSB是1;确保非零初始数组

p = n; //强制gen_state()调用下一个随机数

}

//mtrand.h


#ifndef MTRAND_H

#define MTRAND_H


class MTRand_int32 {// Mersenne Twister随机数发生器

public:

//默认构造函数:仅当这是第一个实例时才使用默认种子

MTRand_int32(){if(!init)seed(5489UL); init = true; } $ / $
//构造函数,32位int作为种子

MTRand_int32(unsigned long s){seed(s); init = true; } $ / $
//构造函数,大小为32位整数数组作为种子

MTRand_int32(const unsigned long * array,int size){seed(array,size);

init = true; }

//两个种子函数

void seed(unsigned long); //具有32位整数的种子

void seed(const unsigned long *,int size); //带数组的种子

//重载operator()使其成为生成器(仿函数)

unsigned long operator()(){return rand_int32(); }

~MTRand_int32(){} //析构函数

protected://派生类使用,否则无法访问;使用

() - 运营商

unsigned long rand_int32(); //生成32位随机整数

private:

static const int n = 624,m = 397; //编译时间常数

//下面的变量是静态的(不存在重复项)

static unsigned long state [n]; //状态向量数组

static int p; //状态数组中的位置

static bool init; //如果调用了init函数,则为true

//用于生成伪随机数的私有函数

unsigned long twiddle(unsigned long,unsigned long); //使用

gen_state()

void gen_state(); //生成新状态

//使复制构造函数和赋值运算符不可用,它们不是
有意义

MTRand_int32(const MTRand_int32& ); //未定义复制构造函数

void operator =(const MTRand_int32&); //赋值运算符未定义

};


//内联速度,因此必须驻留在头文件中

inline unsigned long MTRand_int32 :: twiddle(unsigned long u,unsigned long v)

{

return(((u& 0x80000000UL)|(v& 0x7FFFFFFFUL))> > 1)

^((v& 1UL)?0x9908B0DFUL:0x0UL);

}


inline unsigned long MTRand_int32 :: rand_int32(){//生成32位随机

int

if(p == n)gen_state(); //需要新的状态向量

// gen_state()被拆分为非内联,因为每624次调用只调用一次

//否则irand()会变得太大而无法获得

内联

unsigned long x = state [p ++];

x ^ =(x>> ; 11);

x ^ =(x << 7)& 0x9D2C5680UL;

x ^ =(x << 15)& 0xEFC60000UL;

返回x ^(x>> 18);

}


//生成双浮点数在半开区间[0,1]

级MTRand:public MTRand_int32 {

public:

MTRand():MTRand_int32() {}

MTRand(无符号长种子):MTRand_int32(种子){}

MTRand(const unsigned long * seed,int size):MTRand_int32(种子,大小){ }

~MTRand(){}

double operator()()$

返回static_cast< double>(rand_int32())*( 1. / 4294967296.); } //

除以2 ^ 32

私人:

MTRand(const MTRand&); //未定义的复制构造函数

void operator =(const MTRand&); //赋值运算符未定义

};


//在闭区间[0,1]中生成双浮点数

class MTRand_closed:public MTRand_int32 {

public:

MTRand_closed():MTRand_int32(){}

MTRand_closed(unsigned long seed): MTRand_int32(种子){}

MTRand_closed(const unsigned long * seed,int size):MTRand_int32(种子,

大小){}

~MTRand_closed(){}

double operator()()$

返回static_cast< double>(rand_int32())*(1. / 4294967295.); } //

除以2 ^ 32 - 1

private:

MTRand_closed(const MTRand_closed&); //未定义的复制构造函数

void operator =(const MTRand_closed&); //赋值运算符未定义

};


//在开放区间(0,1)中生成双浮点数

class MTRand_open:public MTRand_int32 {

public:

MTRand_open():MTRand_int32(){}

MTRand_open(unsigned long seed): MTRand_int32(种子){}

MTRand_open(const unsigned long * seed,int size):MTRand_int32(种子,

大小){}

~MTRand_open(){}

double operator()(){

return(static_cast< double>(rand_int32())+ .5)*(1. / 4294967296 ); }

//除以2 ^ 32

私人:

MTRand_open(const MTRand_open&); //未定义的复制构造函数

void operator =(const MTRand_open&); //赋值运算符未定义

};


//在半开区间[0,1]中生成53位分辨率双倍

class MTRand53:public MTRand_int32 {

public:

MTRand53():MTRand_int32(){}

MTRand53(无符号长种子) ):MTRand_int32(种子){}

MTRand53(const unsigned long * seed,int size):MTRand_int32(种子,大小)

{}

~MTRand53(){}

double operator()(){

return(static_cast< double>(rand_int32()>> 5)* 67108864。 +

static_cast< double>(rand_int32()>> 6))*(1. / 9007199254740992.); }

私人:

MTRand53(const MTRand53&); //未定义的复制构造函数

void operator =(const MTRand53&); //赋值运算符未定义

};


#endif // MTRAND_H

** VC ++生成的错误:**


mtrand.cpp

d:\\\mtrand.h(65):错误C2258:非法纯语法,必须是''= 0' '

d:\\\mtrand.h(65):错误C2252:''n'':纯指示符只能为函数指定


d:\\\mtrand.h(65):错误C2258:非法的纯语法,必须是''= 0''

d:\ main \ mtrand.h (65):错误C2252:''m'':纯指示符只能为函数指定



d:\ main \ mtrand.h(67):错误C2065:''n'':未声明的标识符

d:\\\mtrand.h(67):错误C2057:预期的常量表达式

D:\ Main \mtrand.cpp(8):错误C2057:预期的常量表达式

D:\ Main \ mtrand.cpp(8):错误C2466:无法分配常量数组

size 0

D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Main \mtrand.cpp(15):错误C2374:''我':重新定义;多个

初始化

D:\ Main \ mtrand.cpp(13):看到我的声明

D: \ Main \ mtrand.cpp(43):错误C2374:''k'':重新定义;多个

初始化

D:\ Main \ mtrand.cpp(36):看到''k''的声明

执行错误cl.exe。


Main.exe - 11个错误,0个警告

Hi,

I need a way to use random numbers in c++. In my c++ project, when using
the mingw compiler I used a mersenne twister that is publicly available and
this did its job well. Now I have shelled out on VC++ 6.0 compiling that
same code is proving difficult. I am not too worried how I generate random
numbers in c++, as long as it is sufficiently random.

Can anybody help me out in getting what I want from VC++?

If anybody wants the existing code along with errors generated by VC++ then
see below. Perhaps someone can explain what the problem is and how to fix
it. Sorry it is long.

On a separate point, as you might expect it is quite dismoralising to buy
something that promised to be able to compile ISO C++ and then to have
errors thrown at you from code that compiled fine before. I am not talking
about just this code, as I am faced with rewriting an entire project to fit
in with VC++. I''m thinking it is because I did not create the classes from
within VC++, so perhaps I can copy and paste code into the damn thing in
sections - any advice on this? I wanted VC++ to use mfc and make creating a
GUI simple, as well as to take advantage of its debugging facilities and
code hints. Will it really compile exactly the same as mingw compiler used
to in DevC++ or do I need to get a good VC++ book because it is different?
Any suggestions on what to read to get the best out of it?

code:

// mtrand.cpp, see include file mtrand.h for information

#include "mtrand.h"
// non-inline function definitions and static member definitions cannot
// reside in header file because of the risk of multiple declarations

// initialization of static private members
unsigned long MTRand_int32::state[n] = {0x0UL};
int MTRand_int32::p = 0;
bool MTRand_int32::init = false;

void MTRand_int32::gen_state() { // generate new state vector
for (int i = 0; i < (n - m); ++i)
state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]);
for (int i = n - m; i < (n - 1); ++i)
state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]);
state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]);
p = 0; // reset position
}

void MTRand_int32::seed(unsigned long s) { // init by 32 bit seed
state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
for (int i = 1; i < n; ++i) {
state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
// see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
// in the previous versions, MSBs of the seed affect only MSBs of the array
state
// 2002/01/09 modified by Makoto Matsumoto
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
}
p = n; // force gen_state() to be called for next random number
}

void MTRand_int32::seed(const unsigned long* array, int size) { // init by
array
seed(19650218UL);
int i = 1, j = 0;
for (int k = ((n > size) ? n : size); k; --k) {
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) *
1664525UL))
+ array[j] + j; // non linear
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
++j; j %= size;
if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
}
for (int k = n - 1; k; --k) {
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) *
1566083941UL)) - i;
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
}
state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
p = n; // force gen_state() to be called for next random number
}
//mtrand.h

#ifndef MTRAND_H
#define MTRAND_H

class MTRand_int32 { // Mersenne Twister random number generator
public:
// default constructor: uses default seed only if this is the first instance
MTRand_int32() { if (!init) seed(5489UL); init = true; }
// constructor with 32 bit int as seed
MTRand_int32(unsigned long s) { seed(s); init = true; }
// constructor with array of size 32 bit ints as seed
MTRand_int32(const unsigned long* array, int size) { seed(array, size);
init = true; }
// the two seed functions
void seed(unsigned long); // seed with 32 bit integer
void seed(const unsigned long*, int size); // seed with array
// overload operator() to make this a generator (functor)
unsigned long operator()() { return rand_int32(); }
~MTRand_int32() {} // destructor
protected: // used by derived classes, otherwise not accessible; use the
()-operator
unsigned long rand_int32(); // generate 32 bit random integer
private:
static const int n = 624, m = 397; // compile time constants
// the variables below are static (no duplicates can exist)
static unsigned long state[n]; // state vector array
static int p; // position in state array
static bool init; // true if init function is called
// private functions used to generate the pseudo random numbers
unsigned long twiddle(unsigned long, unsigned long); // used by
gen_state()
void gen_state(); // generate new state
// make copy constructor and assignment operator unavailable, they don''t
make sense
MTRand_int32(const MTRand_int32&); // copy constructor not defined
void operator=(const MTRand_int32&); // assignment operator not defined
};

// inline for speed, must therefore reside in header file
inline unsigned long MTRand_int32::twiddle(unsigned long u, unsigned long v)
{
return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
}

inline unsigned long MTRand_int32::rand_int32() { // generate 32 bit random
int
if (p == n) gen_state(); // new state vector needed
// gen_state() is split off to be non-inline, because it is only called once
// in every 624 calls and otherwise irand() would become too big to get
inlined
unsigned long x = state[p++];
x ^= (x >> 11);
x ^= (x << 7) & 0x9D2C5680UL;
x ^= (x << 15) & 0xEFC60000UL;
return x ^ (x >> 18);
}

// generates double floating point numbers in the half-open interval [0, 1)
class MTRand : public MTRand_int32 {
public:
MTRand() : MTRand_int32() {}
MTRand(unsigned long seed) : MTRand_int32(seed) {}
MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
~MTRand() {}
double operator()() {
return static_cast<double>(rand_int32()) * (1. / 4294967296.); } //
divided by 2^32
private:
MTRand(const MTRand&); // copy constructor not defined
void operator=(const MTRand&); // assignment operator not defined
};

// generates double floating point numbers in the closed interval [0, 1]
class MTRand_closed : public MTRand_int32 {
public:
MTRand_closed() : MTRand_int32() {}
MTRand_closed(unsigned long seed) : MTRand_int32(seed) {}
MTRand_closed(const unsigned long* seed, int size) : MTRand_int32(seed,
size) {}
~MTRand_closed() {}
double operator()() {
return static_cast<double>(rand_int32()) * (1. / 4294967295.); } //
divided by 2^32 - 1
private:
MTRand_closed(const MTRand_closed&); // copy constructor not defined
void operator=(const MTRand_closed&); // assignment operator not defined
};

// generates double floating point numbers in the open interval (0, 1)
class MTRand_open : public MTRand_int32 {
public:
MTRand_open() : MTRand_int32() {}
MTRand_open(unsigned long seed) : MTRand_int32(seed) {}
MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed,
size) {}
~MTRand_open() {}
double operator()() {
return (static_cast<double>(rand_int32()) + .5) * (1. / 4294967296.); }
// divided by 2^32
private:
MTRand_open(const MTRand_open&); // copy constructor not defined
void operator=(const MTRand_open&); // assignment operator not defined
};

// generates 53 bit resolution doubles in the half-open interval [0, 1)
class MTRand53 : public MTRand_int32 {
public:
MTRand53() : MTRand_int32() {}
MTRand53(unsigned long seed) : MTRand_int32(seed) {}
MTRand53(const unsigned long* seed, int size) : MTRand_int32(seed, size)
{}
~MTRand53() {}
double operator()() {
return (static_cast<double>(rand_int32() >> 5) * 67108864. +
static_cast<double>(rand_int32() >> 6)) * (1. / 9007199254740992.); }
private:
MTRand53(const MTRand53&); // copy constructor not defined
void operator=(const MTRand53&); // assignment operator not defined
};

#endif // MTRAND_H
**Errors generated by VC++:**

mtrand.cpp
d:\main\mtrand.h(65) : error C2258: illegal pure syntax, must be ''= 0''
d:\main\mtrand.h(65) : error C2252: ''n'' : pure specifier can only be
specified for functions
d:\main\mtrand.h(65) : error C2258: illegal pure syntax, must be ''= 0''
d:\main\mtrand.h(65) : error C2252: ''m'' : pure specifier can only be
specified for functions
d:\main\mtrand.h(67) : error C2065: ''n'' : undeclared identifier
d:\main\mtrand.h(67) : error C2057: expected constant expression
D:\Main\mtrand.cpp(8) : error C2057: expected constant expression
D:\Main\mtrand.cpp(8) : error C2466: cannot allocate an array of constant
size 0
D:\Main\mtrand.cpp(13) : error C2065: ''m'' : undeclared identifier
D:\Main\mtrand.cpp(15) : error C2374: ''i'' : redefinition; multiple
initialization
D:\Main\mtrand.cpp(13) : see declaration of ''i''
D:\Main\mtrand.cpp(43) : error C2374: ''k'' : redefinition; multiple
initialization
D:\Main\mtrand.cpp(36) : see declaration of ''k''
Error executing cl.exe.

Main.exe - 11 error(s), 0 warning(s)


推荐答案

Jason< ja *********** @ btinternet.com>这样说:
Jason <ja***********@btinternet.com> spoke thus:
我需要一种在c ++中使用随机数的方法。在我的c ++项目中,当使用mingw编译器时,我使用了一种公开可用的mersenne twister,这很好地完成了它的工作。现在我已经讨论了VC ++ 6.0编译,相同的代码证明是困难的。我不太担心如何在c ++中生成随机数字,只要它足够随机。
有人可以帮助我从VC ++中得到我想要的东西吗?
I need a way to use random numbers in c++. In my c++ project, when using
the mingw compiler I used a mersenne twister that is publicly available and
this did its job well. Now I have shelled out on VC++ 6.0 compiling that
same code is proving difficult. I am not too worried how I generate random
numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?




我不知道它是否能帮助你,但是ANSI C随机数生成器

几周前发布到comp.lang.c.你可能想看看

out。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



I don''t know if it''ll help you, but an ANSI C random number generator
was posted a few weeks ago to comp.lang.c. You may want to check that
out.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


" Jason" < JA *********** @ btinternet.com>在消息中写道

news:c1 ********** @ hercules.btinternet.com ...
"Jason" <ja***********@btinternet.com> wrote in message
news:c1**********@hercules.btinternet.com...

我需要一种在c ++中使用随机数的方法。 [snip]
Hi,

I need a way to use random numbers in c++. [snip]




你可以试试UVS。你可以从我的签名中获取它,它适用于6.0。


-

Cy
http://home.rochester.rr.com/cyhome/


-----开始PGP签名消息-----

哈希:SHA1


杰森,

也许我错过了一些东西,但是不会(或)
stdlib.h工作?如果你不喜欢这样,你可以随时尝试Boost''s
http://boost.org/libs/random/index.html

-----开始PGP SIGNATURE -----

版本:GnuPG v1.0.6(GNU / Linux)

评论:将GnuPG与Mozilla一起使用 - http://enigmail.mozdev.org

iD8DBQFAP9acoo / Prlj9GScRAk / sAJ0V + F9 / 7RPiWbNnyOiOq7qZKvWEUQCfc9bt

L81DJIiu7WMg8uD / Bs5xrXg =

= 4J93

----- END PGP SIGNATURE -----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

Perhaps I''m missing something, but wouldn''t rand() or srand() of
stdlib.h work? if you don''t like that, you could always try Boost''s
http://boost.org/libs/random/index.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAP9acoo/Prlj9GScRAk/sAJ0V+F9/7RPiWbNnyOiOq7qZKvWEUQCfc9bt
L81DJIiu7WMg8uD/Bs5xrXg=
=4J93
-----END PGP SIGNATURE-----


这篇关于随机数码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆