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