Re: Implementing the KISS4691 RNG

Discussion in 'C++' started by Dann Corbit, Sep 8, 2010.

  1. Dann Corbit

    Dann Corbit Guest

    In article <>, says...
    >
    > Dann Corbit <> writes:
    > [...]
    > > c:\tmp\kiss7.c(46) : warning C4701: potentially uninitialized local
    > > variable 'x' used

    > [...]
    > > P.S.
    > > Is C4701 up above the dumbest warning you ever saw in your life?
    > > */

    >
    > Um, no. It merely means that the compiler wasn't able to prove that x
    > is always initialized before it's used. You can probably silence it by
    > providing an initializer on the declaration.
    >
    > For example, if I wrote something like this:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int main(void) {
    > int x;
    > srand(time(NULL));
    > if (rand() % 2 == 0) {
    > puts("initializing x");
    > x = 42;
    > }
    > printf("x = %d\n", x);
    > return 0;
    > }
    >
    > I'd certainly like my compiler to warn me about it.
    >
    > (Interestingly, gcc doesn't warn with "-Wall -Wextra", but at -O1
    > and higher x is 42 whether the message is printed or not. Apparently
    > the compiler recognized the undefined behavior but didn't bother to
    > tell me about it.)
    >
    > Why do you find the warning dumb?


    Because the loops absolutely, positively must execute at least once.

    > Followups redirected to comp.lang.c.


    My attempt at a C++ version failed. I am not sure what is wrong with
    the second method.


    class MWC {

    private:
    unsigned long xs;
    unsigned long xcng;
    unsigned long Q[4691];
    unsigned long t;
    unsigned long x;
    unsigned long c;
    unsigned long j;

    public:

    // pseudo-random number method:
    unsigned long kissRandom(void) {
    j = (j < 4690) ? j + 1 : 0;
    x = Q[j];
    t = (x << 13) + c;
    if (t < c) {
    c = (x >> 19) + 1;
    t = t + x;
    } else {
    t = t + x;
    c = (x >> 19) + (t < x);
    }
    return (Q[j] = t);
    }

    // another flavor, with a bit of mixing:
    unsigned long kissRandom2(void) {
    return (kissRandom() + (xcng = 69069 * xcng + 123) +
    (xs ^= (xs << 13), xs ^= (xs >> 17),
    xs ^= (xs << 5)));
    }

    // Constructor:
    MWC(void) {
    unsigned long i;
    c = 0;
    j = 4691;
    xs = 521288629;
    long xcng = 362436069;
    for (i = 0; i < sizeof Q / sizeof Q[0]; i++)
    Q = (xcng = 69069 * xcng + 123) +
    (xs ^= (xs << 13), xs ^= (xs >> 17),
    xs ^= (xs << 5));
    }
    };

    #ifdef UNIT_TEST

    #include <iostream>

    int main()
    {
    unsigned long i;
    unsigned long x;
    MWC prng;

    std::cout << "Does MWC result=3740121002 ? " << std::endl;
    for (i = 0; i < 1000000000; i++)
    x = prng.kissRandom();
    std::cout << x << std::endl;

    std::cout << "Does KISS result=2224631993 ?" << std::endl;
    for (i = 0; i < 1000000000; i++)
    x = prng.kissRandom2();
    std::cout << x << std::endl;
    return 0;
    }
    #endif
    /*
    c:\tmp>cl /EHsc /DUNIT_TEST /W4 /Ox kiss7.cpp
    Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64
    Copyright (C) Microsoft Corporation. All rights reserved.

    kiss7.cpp
    c:\tmp\kiss7.cpp(60) : warning C4701: potentially uninitialized local
    variable 'x' used
    Microsoft (R) Incremental Linker Version 9.00.30729.01
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:kiss7.exe
    kiss7.obj

    c:\tmp>kiss7
    Does MWC result=3740121002 ?
    3740121002
    Does KISS result=2224631993 ?
    885060915
    */
     
    Dann Corbit, Sep 8, 2010
    #1
    1. Advertising

  2. Dann Corbit

    BartC Guest

    "Dann Corbit" <> wrote in message
    news:-september.org...

    > std::cout << x << std::endl;


    You'd think that would all it's capabilities, C++ would allow you to devise
    a way of writing just:

    print x

    --
    Bartc
     
    BartC, Sep 9, 2010
    #2
    1. Advertising

  3. On 9/9/2010 5:04 AM, BartC wrote:
    > "Dann Corbit" <> wrote in message
    > news:-september.org...
    >
    >> std::cout << x << std::endl;

    >
    > You'd think that would all it's capabilities, C++ would allow you to
    > devise a way of writing just:
    >
    > print x


    I think you meant

    println x

    and in fact you can, if you define the macro ('println') that would
    expand to

    somecustomoutputter(std::cout) <<

    and which would output newline and purge its buffer upon every output.
    You're obviously not interested in that, so I won't bore you with the
    details.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 9, 2010
    #3
    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. Francois Grieu
    Replies:
    7
    Views:
    441
    Ben Bacarisse
    Apr 8, 2009
  2. geo
    Replies:
    87
    Views:
    2,080
    Michael Press
    Dec 6, 2010
  3. geo
    Replies:
    5
    Views:
    356
    Tim Little
    Aug 30, 2010
  4. geo

    Implementing the KISS4691 RNG

    geo, Sep 5, 2010, in forum: C Programming
    Replies:
    22
    Views:
    780
    David Thompson
    Sep 17, 2010
  5. robin
    Replies:
    79
    Views:
    1,264
    Frederick Williams
    Jul 1, 2011
Loading...

Share This Page