Random number generator for use with random_shuffle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Generic Usenet Account

    Random number generator for use with random_shuffle

    I had a need to create my own RandomNumberGen erator class, for use with
    random_shuffle (I did not want the same sequence generated each time).
    I looked up an example provided by Nicolai M. Josuttis, in his
    fantastic book : The C++ Standard Library - A Tutorial and Reference
    (http://www.josuttis.com/libbook/). Here is what he had:

    class MyRandom {
    public:
    ptrdiff_t operator() (ptrdiff_t max) {
    double tmp;
    tmp = static_cast<dou ble>(rand())
    / static_cast<dou ble>(RAND_MAX);
    return static_cast<ptr diff_t>(tmp * max);
    }
    };



    I adapted it as follows:
    struct RandomNumberGen erator
    {
    ptrdiff_t operator() (ptrdiff_t diffVal)
    {
    struct timeval tod;
    gettimeofday(&t od, 0);
    srand(tod.tv_se c + tod.tv_usec);
    int randVal = rand();

    double retVal;
    retVal = static_cast<dou ble>(randVal) /
    static_cast<dou ble>(RAND_MAX);
    return static_cast<ptr diff_t>(retVal * diffVal);
    }
    };


    Now here's the funny part. If I don't divide retVal by
    static_cast<dou ble>(RAND_MAX), I am getting a core dump.


    Any ideas what could be causing this?

    A simple program that operates on a text file and randomly shuffles its
    lines follows. I am able to reproduce the coredump with this simple
    program

    Thx,
    Song


    //~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~

    #include <iostream>
    #include <fstream>
    #include <iterator>
    #include <string>
    #include <vector>
    #include <sys/time.h>

    using namespace std;

    /////////////////////////////////////////////////////////////
    //
    /////////////////////////////////////////////////////////////
    struct RandomNumberGen erator
    {
    #define CAUSE_COREDUMP

    #ifdef CAUSE_COREDUMP
    ptrdiff_t operator() (ptrdiff_t diffVal)
    {
    struct timeval tod;
    gettimeofday(&t od, 0);
    srand(tod.tv_se c + tod.tv_usec);
    int randVal = rand();

    double retVal;
    retVal = static_cast<dou ble>(randVal);
    return static_cast<ptr diff_t>(retVal * diffVal);
    }
    #else // no core-dump
    ptrdiff_t operator() (ptrdiff_t diffVal)
    {
    struct timeval tod;
    gettimeofday(&t od, 0);
    srand(tod.tv_se c + tod.tv_usec);
    int randVal = rand();

    double retVal;
    retVal = static_cast<dou ble>(randVal) /
    static_cast<dou ble>(RAND_MAX);
    return static_cast<ptr diff_t>(retVal * diffVal);
    }
    #endif // CAUSE_COREDUMP
    };

    /////////////////////////////////////////////////////////////
    //
    /////////////////////////////////////////////////////////////
    int
    main(int argc, char* argv[])
    {
    if(argc == 1)
    {
    cerr << "usage: " << argv[0] << " <input-file>" << endl;
    return 1;
    }


    ifstream ifs(argv[1]);
    if(!ifs)
    {
    cerr << "Cannot open file " << argv[1] << " ....exiting" << endl;
    return 2;
    }

    string line;

    vector<stringli neColl;

    while(getline(i fs, line))
    {
    if(line.empty() ) continue;

    lineColl.push_b ack(line);
    }

    if(!lineColl.em pty())
    {
    RandomNumberGen erator rng;
    random_shuffle( lineColl.begin( ), lineColl.end(), rng);

    // print each entry in the collection on a separate line
    copy (lineColl.begin (), lineColl.end(), ostream_iterato r<string>
    (cout, "\n"));
    }

    return 0;
    }

  • =?iso-8859-1?q?Erik_Wikstr=F6m?=

    #2
    Re: Random number generator for use with random_shuffle

    On Dec 17, 7:44 pm, "Generic Usenet Account" <use...@sta.sam sung.com>
    wrote:
    I had a need to create my own RandomNumberGen erator class, for use with
    random_shuffle (I did not want the same sequence generated each time).
    I looked up an example provided by Nicolai M. Josuttis, in his
    fantastic book : The C++ Standard Library - A Tutorial and Reference
    (http://www.josuttis.com/libbook/). Here is what he had:
    >
    class MyRandom {
    public:
    ptrdiff_t operator() (ptrdiff_t max) {
    double tmp;
    tmp = static_cast<dou ble>(rand())
    / static_cast<dou ble>(RAND_MAX);
    return static_cast<ptr diff_t>(tmp * max);
    }
    >
    };I adapted it as follows:
    struct RandomNumberGen erator
    {
    ptrdiff_t operator() (ptrdiff_t diffVal)
    {
    struct timeval tod;
    gettimeofday(&t od, 0);
    srand(tod.tv_se c + tod.tv_usec);
    int randVal = rand();
    >
    double retVal;
    retVal = static_cast<dou ble>(randVal) /
    static_cast<dou ble>(RAND_MAX);
    return static_cast<ptr diff_t>(retVal * diffVal);
    }
    >
    };Now here's the funny part. If I don't divide retVal by
    static_cast<dou ble>(RAND_MAX), I am getting a core dump.
    >
    Any ideas what could be causing this?
    >
    A simple program that operates on a text file and randomly shuffles its
    lines follows. I am able to reproduce the coredump with this simple
    program
    If you want help the FAQ stipulates that you should post a minimal
    functional example, however I could not get your example working
    without modifications. There were at least two problems with your code,
    first <sys/time.his a non-standard header that does not exist on my
    system, second you need to include <algorithm>. After some
    modifications I was able to compile and run the program, however I did
    not get any errors, so either my modifications removed the problem or
    it's something specific to your platform/environment.

    I suggest that you try again to minimize the problem using only
    standard code, you might want to have a look at <ctime>, you could also
    put the #ifdef/#else/#endif just around the code-line that differs
    instead of the whole body of the function.

    --
    Erik Wikström

    Comment

    Working...