Random number generator for use with random_shuffle

Discussion in 'C++' started by Generic Usenet Account, Dec 17, 2006.

  1. I had a need to create my own RandomNumberGenerator 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<double>(rand())
    / static_cast<double>(RAND_MAX);
    return static_cast<ptrdiff_t>(tmp * max);
    }
    };



    I adapted it as follows:
    struct RandomNumberGenerator
    {
    ptrdiff_t operator() (ptrdiff_t diffVal)
    {
    struct timeval tod;
    gettimeofday(&tod, 0);
    srand(tod.tv_sec + tod.tv_usec);
    int randVal = rand();

    double retVal;
    retVal = static_cast<double>(randVal) /
    static_cast<double>(RAND_MAX);
    return static_cast<ptrdiff_t>(retVal * diffVal);
    }
    };


    Now here's the funny part. If I don't divide retVal by
    static_cast<double>(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 RandomNumberGenerator
    {
    #define CAUSE_COREDUMP

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

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

    double retVal;
    retVal = static_cast<double>(randVal) /
    static_cast<double>(RAND_MAX);
    return static_cast<ptrdiff_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<string> lineColl;

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

    lineColl.push_back(line);
    }

    if(!lineColl.empty())
    {
    RandomNumberGenerator rng;
    random_shuffle(lineColl.begin(), lineColl.end(), rng);

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

    return 0;
    }
     
    Generic Usenet Account, Dec 17, 2006
    #1
    1. Advertising

  2. On Dec 17, 7:44 pm, "Generic Usenet Account" <>
    wrote:
    > I had a need to create my own RandomNumberGenerator 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<double>(rand())
    > / static_cast<double>(RAND_MAX);
    > return static_cast<ptrdiff_t>(tmp * max);
    > }
    >
    > };I adapted it as follows:
    > struct RandomNumberGenerator
    > {
    > ptrdiff_t operator() (ptrdiff_t diffVal)
    > {
    > struct timeval tod;
    > gettimeofday(&tod, 0);
    > srand(tod.tv_sec + tod.tv_usec);
    > int randVal = rand();
    >
    > double retVal;
    > retVal = static_cast<double>(randVal) /
    > static_cast<double>(RAND_MAX);
    > return static_cast<ptrdiff_t>(retVal * diffVal);
    > }
    >
    > };Now here's the funny part. If I don't divide retVal by
    > static_cast<double>(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.h> is 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 18, 2006
    #2
    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. Peter Ammon

    Re: random_shuffle seed

    Peter Ammon, Oct 31, 2003, in forum: C++
    Replies:
    0
    Views:
    462
    Peter Ammon
    Oct 31, 2003
  2. Peter Ammon

    Re: random_shuffle seed

    Peter Ammon, Oct 31, 2003, in forum: C++
    Replies:
    1
    Views:
    1,668
    Chris Theis
    Oct 31, 2003
  3. steflhermitte
    Replies:
    1
    Views:
    826
    Kanenas
    Apr 21, 2005
  4. globalrev
    Replies:
    4
    Views:
    768
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,173
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page