By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Random number generator for use with random_shuffle

P: n/a
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<stringlineColl;

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

Dec 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Dec 17, 7:44 pm, "Generic Usenet Account" <use...@sta.samsung.com>
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.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

Dec 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.