Random number generator for use with random_shuffle

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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;
}
 
?

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top