Re: random number code

Discussion in 'C++' started by Chris Theis, Feb 28, 2004.

  1. Chris Theis

    Chris Theis Guest

    "Jason" <> wrote in message
    news:c1obqj$8o6$...
    > 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
     
    Chris Theis, Feb 28, 2004
    #1
    1. Advertising

  2. Chris Theis

    Jason Guest

    "Chris Theis" <> wrote in message
    news:c1q1gg$omu$...

    > 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
    >
    >
     
    Jason, Feb 28, 2004
    #2
    1. Advertising

  3. Chris Theis

    Pete Becker Guest

    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)
     
    Pete Becker, Feb 28, 2004
    #3
  4. Chris Theis

    Jason Guest


    > 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.
     
    Jason, Feb 29, 2004
    #4
  5. Chris Theis

    Chris Theis Guest

    "Jason" <> schrieb im Newsbeitrag
    news:c1r09k$enf$...
    [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
     
    Chris Theis, Mar 1, 2004
    #5
  6. Chris Theis

    Greg Schmidt Guest

    On Sat, 28 Feb 2004 21:13:24 +0000 (UTC), Jason wrote:

    > "Chris Theis" <> wrote in message
    > news:c1q1gg$omu$...
    >
    >> 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-us/vclang/html/vclrfStandardComplianceIssuesInVisualC.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...ml/vclrfStandardComplianceIssuesInVisualC.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
    Trawna Publications http://www.trawna.com/
     
    Greg Schmidt, Mar 3, 2004
    #6
  7. 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
     
    Joe Hotchkiss, Mar 3, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xeys_00
    Replies:
    12
    Views:
    856
    Jonathan Arnold
    Apr 11, 2005
  2. Sweety
    Replies:
    6
    Views:
    4,071
    CBFalconer
    Apr 5, 2004
  3. globalrev
    Replies:
    4
    Views:
    774
    Gabriel Genellina
    Apr 20, 2008
  4. Alex Untitled
    Replies:
    11
    Views:
    661
    Giampiero Zanchi
    Nov 16, 2009
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page