Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: random number code

Reply
Thread Tools

Re: random number code

 
 
Chris Theis
Guest
Posts: n/a
 
      02-28-2004

"Jason" <(E-Mail Removed)> wrote in message
news:c1obqj$8o6$(E-Mail Removed)...
> 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++?


Yes.

>
> 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++.


Well the topic of ISO compiler compliance is a long and dirty road. If you
want to stick to MS then I'd suggest to use version 7.1 as 6.0 is quite
outdated and carries some serious legacies. (I think the compliance level
dates back to pre 1999, but don't pin me down on that!). However, the
problem actually is in your code and not with the compiler.

[SNIP]
> unsigned long rand_int32(); // generate 32 bit random integer
> private:
> static const int n = 624, m = 397; // compile time constants


And here we go. You can't declare and initialize these variables within the
class in one statement. Move the initialization out of the class scope and
you're fine.

> // the variables below are static (no duplicates can exist)
> static unsigned long state[n]; // state vector array


Replace [n] with [624] as the initialization is not available at this point.

[SNIP]

And that's it. One thing which is really a compiler shortcoming is the
"supposed" redifinition of variables used as counters in a for loop, but
this can easily be fixed by removing the redundant declarations.

HTH
Chris


 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      02-28-2004

"Chris Theis" <(E-Mail Removed)> wrote in message
news:c1q1gg$omu$(E-Mail Removed)...

> Well the topic of ISO compiler compliance is a long and dirty road. If you
> want to stick to MS then I'd suggest to use version 7.1 as 6.0 is quite
> outdated and carries some serious legacies. (I think the compliance level
> dates back to pre 1999, but don't pin me down on that!). However, the
> problem actually is in your code and not with the compiler.



Thanks for the corrections Chris, great message. Clearly it is in my
interests for you to expand on the point regarding "serious legacies". The
money is spent if I resold the software I would be losing good money. I
only wanted something to compile my existing c++ code and to provide a user
interface for it. It has singularly failed to do the former, I have not
tested it on the latter point. Any websites where its problems are
documented or any advice you can offer? Thanks again.

>
> [SNIP]
> > unsigned long rand_int32(); // generate 32 bit random integer
> > private:
> > static const int n = 624, m = 397; // compile time constants

>
> And here we go. You can't declare and initialize these variables within

the
> class in one statement. Move the initialization out of the class scope and
> you're fine.
>
> > // the variables below are static (no duplicates can exist)
> > static unsigned long state[n]; // state vector array

>
> Replace [n] with [624] as the initialization is not available at this

point.
>
> [SNIP]
>
> And that's it. One thing which is really a compiler shortcoming is the
> "supposed" redifinition of variables used as counters in a for loop, but
> this can easily be fixed by removing the redundant declarations.
>


I am glad you fixed this because as indicated in other messages on here,
there is a view somewhere that it is poor practice to use rand and srand
when you require randomness in a serious way. This is due to the poor
quality of randomness. I have no idea whether what I have read has any
authority but that is why I sought alternatives.

> HTH
> Chris
>
>



 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      02-28-2004
Jason wrote:
>
> I am glad you fixed this because as indicated in other messages on here,
> there is a view somewhere that it is poor practice to use rand and srand
> when you require randomness in a serious way.
> >


When you require randomness in a serious way it is poor practice to use
any random number generator until you have carefully analyzed its
properties.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      02-29-2004

> When you require randomness in a serious way it is poor practice to use
> any random number generator until you have carefully analyzed its
> properties.


Thanks for pointing out the obvious.



 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      03-01-2004

"Jason" <(E-Mail Removed)> schrieb im Newsbeitrag
news:c1r09k$enf$(E-Mail Removed)...
[SNIP]
>
> Thanks for the corrections Chris, great message. Clearly it is in my
> interests for you to expand on the point regarding "serious legacies".

The
> money is spent if I resold the software I would be losing good money.

I
> only wanted something to compile my existing c++ code and to provide a

user
> interface for it. It has singularly failed to do the former, I have not
> tested it on the latter point. Any websites where its problems are
> documented or any advice you can offer? Thanks again.


Sorry but I can't give you a website summing up all the shortcomings of VC
6.0. However, the support of templates (especially with regard to partial
specialization, nested templates, etc.) is quite poor. Thus, I'd recommend
to upgrade to version 7.1.

[SNIP]
>
> I am glad you fixed this because as indicated in other messages on here,
> there is a view somewhere that it is poor practice to use rand and srand
> when you require randomness in a serious way. This is due to the poor
> quality of randomness. I have no idea whether what I have read has any
> authority but that is why I sought alternatives.
>


If you need more in detail information about random numbers and the quality
of "randomness" IMHO the book by Donald Knuth (The Art of Computer
Programming,Volume 2) is the place to start with.

HTH
Chris


 
Reply With Quote
 
Greg Schmidt
Guest
Posts: n/a
 
      03-03-2004
On Sat, 28 Feb 2004 21:13:24 +0000 (UTC), Jason wrote:

> "Chris Theis" <(E-Mail Removed)> wrote in message
> news:c1q1gg$omu$(E-Mail Removed)...
>
>> Well the topic of ISO compiler compliance is a long and dirty road. If you
>> want to stick to MS then I'd suggest to use version 7.1 as 6.0 is quite
>> outdated and carries some serious legacies. (I think the compliance level
>> dates back to pre 1999, but don't pin me down on that!). However, the
>> problem actually is in your code and not with the compiler.

>
> Thanks for the corrections Chris, great message. Clearly it is in my
> interests for you to expand on the point regarding "serious legacies". The
> money is spent if I resold the software I would be losing good money. I
> only wanted something to compile my existing c++ code and to provide a user
> interface for it. It has singularly failed to do the former, I have not
> tested it on the latter point.


Did you get the answer you were looking for from the rest of Chris' message
(which I have left in below)? It sounds from this paragraph like you might
not have grasped that the problem VC++ has in compiling your old code is
that your old code is not actually ANSI compliant. VC++, and all other
compilers, should refuse to compile it. Put another way, "successful"
compilation of your code should be considered a compiler bug.

> Any websites where its problems are
> documented or any advice you can offer? Thanks again.


http://msdn.microsoft.com/library/en...sInVisualC.asp
has a list of things that were improved in VC++7.1 (aka .NET 2003). As I
understand it, the move from 6.x to 7.0 was essentially a facelift, they
are very similar in terms of standard compliance. 7.0 to 7.1 was where
most of the standards issues were addressed.

It looks like a list of compliance issues in version 6.x or 7.0 is at
http://www.microsoft.com/japan/msdn/...sInVisualC.asp
Despite the URL, very little of this page is actually in Japanese.

>>> unsigned long rand_int32(); // generate 32 bit random integer
>>> private:
>>> static const int n = 624, m = 397; // compile time constants

>>
>> And here we go. You can't declare and initialize these variables within
>> the
>> class in one statement. Move the initialization out of the class scope and
>> you're fine.
>>
>>> // the variables below are static (no duplicates can exist)
>>> static unsigned long state[n]; // state vector array

>>
>> Replace [n] with [624] as the initialization is not available at this
>> point.


That is a highly unfortunate side effect. Sucks to have that 624 repeated
in two places; it's almost sure to lead to an error when one is changed and
the other is missed. Perhaps better to use
static std::vector<unsigned long> state;
and then
state.resize(n);
in the constructor?

--
Greg Schmidt http://www.velocityreviews.com/forums/(E-Mail Removed)
Trawna Publications http://www.trawna.com/
 
Reply With Quote
 
Joe Hotchkiss
Guest
Posts: n/a
 
      03-03-2004
Greg Schmidt wrote:
>>>> unsigned long rand_int32(); // generate 32 bit random integer
>>>> private:
>>>> static const int n = 624, m = 397; // compile time constants
>>>
>>> And here we go. You can't declare and initialize these variables
>>> within
>>> the
>>> class in one statement. Move the initialization out of the class
>>> scope and
>>> you're fine.
>>>
>>>> // the variables below are static (no duplicates can exist)
>>>> static unsigned long state[n]; // state vector array
>>>
>>> Replace [n] with [624] as the initialization is not available at
>>> this
>>> point.

>
> That is a highly unfortunate side effect. Sucks to have that 624
> repeated
> in two places; it's almost sure to lead to an error when one is
> changed and
> the other is missed. Perhaps better to use
> static std::vector<unsigned long> state;
> and then
> state.resize(n);
> in the constructor?


Instead of duplicating the 624 with
static const int n = 624, m = 397;
static unsigned long state[624];
you could write
enum {n = 624, m = 397};
static unsigned long state[n];

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
My random number is only random for the first run??? xeys_00 C++ 12 04-11-2005 03:58 PM
how can i write C code that generate random number without using built in code Sweety C Programming 6 04-04-2004 11:24 PM



Advertisments