Chapter 7: Random Numbers
amirvahid
05-12-2009, 07:51 PM
Dear All,
A relatively simple but accurate method for generating uniformly distributed random numbers in (0,1) range has been described in pre92="Numerical Recipes in C, 1992, Cambridge University Press". I know that these methods have been "shrouded in antiquity" according to rap04="the Art of Molecular Dynamic Simulation, D.C. Rapaport, 2004, Cambridge University Press". However, the general procedure as stated in rap04 in Linux is as follows:
#defineMASK2147483647
#define SCALE 0.4656612873e-9
intrandSeedP=17;
real RandR()
{
randSeedP=(randSeedP*IMUL+IADD)&MASK; return(randSeedP*SCALE);
}
The program considers the “ranSeed” variable as an input data. As mentioned in NR3=[”Numerical Recipes, 3rd Ed”], and in rap04, initialization of the random number sequence used this value to set ranSeedP (so that the run will be reproducible), or, if it set to zero value, a (difficult to reproduce the run) value based on the system clock is chosen,
void InitRand (int randSeedI)
{
struct timeval tv;
if (randSeedI != 0) randSeedP = randSeedI;
else {
gettimeofday (&tv, 0);
randSeedP = tv.tv_usec;
}
}
The program considers the “ranSeed” variable as an input data. As mentioned in NR3=[”Numerical Recipes, 3rd Ed”], and in rap04, initialization of the random number sequence used this value to set ranSeedP (so that the run will be reproducible), or, if it set to zero value, a (difficult to reproduce the run) value based on the system clock is chosen,
void InitRand (int randSeedI)
{
struct timeval tv;
if (randSeedI != 0) randSeedP = randSeedI;
else {
gettimeofday (&tv, 0);
randSeedP = tv.tv_usec;
}
}
It is also possible to generate random vectors such as unit vectors in 3 dimensions with uniformly distributed random orientation, using a rejection method mar72=[Marsaglia, G., Choosing a point from the surface of a sphere, Ann. Math. Stat. 43(1972)645], rap04
The code is easy to write as follows:
void VRand (VecR *p)
{
real s, x, y;
s = 2.;
while (s > 1.) {
x = 2. * RandR () - 1.;
y = 2. * RandR () - 1.;
s = Sqr (x) + Sqr (y);
} 10
p->z = 1. - 2. * s;
s = 2. * sqrt (1. - s);
p->x = s * x;
p->y = s * y;
}
The average success rate at each attempt is pi/4 ~76% and for 2 dimensional vectors
void VRand (VecR *p)
{
real s;
s = 2. * M_PI * RandR ();
p->x = cos (s);
p->y = sin (s);
Note that I have also enclosed the rap04 codes into this thread.
As you see, some of these methods are old as mentioned in NR3 and some of are written in a linux based format (such as gettimeofday (&tv, 0)). I wonder if you could help me with the current version of NR3 and provide some sample codes to be able to generate these random numbers in Visual c++ 2005, Windows XP.
Thanks
Amir
davekw7x
05-13-2009, 10:41 AM
...I wonder if you could help me with the current version of NR3...
You can use a Ran object to generate (approximately) uniformly distributed samples. Like the RandR() function of your example, the Ran::doub() function returns values between zero and 1.
//
// Illustration of nr3 random number generator
// in ran.h
//
// I just generate a bunch of samples between zero and one
// and keep track of minimum and maximum values and calculate
// the average value.
//
// davekw7x
//
// Put the path name of the nr3 code files for your installation
#include "../code/nr3.h"
#include "../code/ran.h"
int main()
{
// Use time(0) to seed the random number generator with
// current system time in seconds
//Ran ran(time(0)); // Create a Ran object
//
// Use your favorite lucky number to give it a seed.
// The same sequence will be generated for every program run,
// and this is (sometimes) helpful for debugging and/or
// comparison purposes.
//
Ran ran(12345678); // Create a Ran object
Doub x;
Doub xmin, xmax, sum;
Int num = 1000000; // Will generate this many samples
Int i;
x = ran.doub(); // First sample;
xmin = x;
xmax = x;
for (i = 1; i < num; i++) {
x = ran.doub();
sum += x;
if (x > xmax) {
xmax = x;
}
if (x < xmin) {
xmin = x;
}
}
cout << scientific << setprecision(16);
cout << "xmin = " << xmin << ", xmax = " << xmax << endl << endl;
cout << "sum = " << sum << ", avg = " << sum/num << endl;
return 0;
}
Output from the program compiled with Visual C++ on Windows XP:
xmin = 1.2680700009868478e-006, xmax = 9.9999918037993141e-001
sum = 5.0009016486706503e+005, avg = 5.0009016486706503e-001
Regards,
Dave
Footnote:
"...random number generators should not be generated with
a method chosen at random. Some theory should be used."
---Donald Knuth
The art of Computer Programming
Volume 2: Seminumerical Algorithms
davekw7x
05-13-2009, 12:04 PM
...
It is also possible to generate random vectors such as unit vectors in 3 dimensions with uniformly distributed random orientation...and for 2 dimensional vectors...
#include "../code/nr3.h"
#include "../code/ran.h"
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884197169399375 105820974944592307816406286
#endif
struct Point2d {
Doub x;
Doub y;
};
// Generate uniformly distributed "random" points on a unit circle
Point2d ran_cir(Ran &r)
{
// Random number between zero and 2*Pi
Doub s = 2.0 * M_PI * r.doub();
Point2d point;
point.x = cos(s);
point.y = sin(s);
return point;
}
struct Point3d {
Doub x;
Doub y;
Doub z;
};
// Generate uniformly distributed "random" points on a unit sphere
Point3d ran_sph(Ran &r)
{
Point3d p;
Doub x, y;
Doub s = 2;
while (s > 1) {
// x and y are random numbers between -1 and 1
x = 2.0 * r.doub() - 1.0;
y = 2.0 * r.doub() - 1.0;
s = x*x + y*y;
}
p.z = 1.0 - 2.0 * s;
s = 2.0 * sqrt(1.0 - s);
p.x = s*x;
p.y = s*y;
return p;
}
int main()
{
// Use a seed that is system time in seconds
// Ran ran(time(0));
// Use a fixed seed for debugging and comparison
Ran ran(12345678);
Int i;
Int n = 10;
cout << fixed << setprecision(6) << showpos;
cout << "Random points on a unit circle:" << endl;
Point2d p2;
for (i = 0; i < n; i++) {
p2 = ran_cir(ran);
cout << " ("<< p2.x << "," << p2.y << ")" << endl;
}
cout << endl;
Point3d p3;
cout << "Random points on a unit sphere:" << endl;
for (i = 0; i < n; i++) {
p3 = ran_sph(ran);
cout << " ("<< p3.x << "," << p3.y << "," << p3.z << ")" << endl;
}
return 0;
}
Output from program compiled with Visual C++ on Windows XP:
Random points on a unit circle:
(-0.718679,-0.695342)
(+0.748440,+0.663203)
(+0.156825,-0.987626)
(-0.949105,+0.314960)
(-0.942018,+0.335561)
(-0.218545,-0.975827)
(-0.817444,-0.576008)
(-0.978275,-0.207310)
(+0.972246,+0.233962)
(+0.838727,+0.544552)
Random points on a unit sphere:
(+0.617248,-0.449934,-0.645418)
(+0.715829,+0.632776,-0.295267)
(+0.761518,+0.508772,-0.401548)
(+0.451914,+0.401696,-0.796501)
(+0.220023,+0.198001,-0.955189)
(+0.608913,+0.657362,+0.443960)
(-0.754495,+0.402076,-0.518722)
(-0.733252,-0.467623,+0.493630)
(+0.440901,-0.890450,+0.112720)
(-0.489854,+0.703041,-0.515534)
Regards,
Dave
amirvahid
05-13-2009, 02:21 PM
Thanks Dave,
Your codes are really helpful.
Yes as you mentioned choosing the random method should not be at random. However, when you people use Molecular Dynamics method, the simpler the random number method the more powerful the algorithm is. But for Monte Carlo methods choosing the random numbers and their generation methods are extremely important and I will also ask my questions about them in the future posts.
Warm wishes
Amir
davekw7x
05-13-2009, 04:33 PM
...the simpler the random number method the more powerful the algorithm is...
I assume that by "simpler" you mean "faster." If you know for sure that the samples generated by a linear congruential generator like the one in your original post are adequately "random," or if you want to compare results, here is a non-NR translation to a form that you should be able to compile with Visual C++. I have written it as a C program, but does not need any changes to let it be compiled as C++:
/*
* Example of linear congruential random number generator
*
* Should work with any compiler that has 32-bit ints.
*
* Tested with current compilers from Microsoft, Borland, GNU
*
* davekw7x
*
* If your compiler has 16-bit ints, either
* 1. Scrap it and upgrade to a more modern one.
* or
* 2. Try the program using long ints instead of ints
*
* I vote for number 1.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h> /* needed if NDIM is defined to be 2 or 3 */
#include <time.h>
#ifndef INT_MAX
#define INT_MAX 0x7fffffff
#endif
/*
* As a matter of style, I replace the following #defined macros with consts
*/
/*#define IADD 453806245 */
/*#define IMUL 314159269 */
/*#define MASK 2147483647 */
/*#define SCALE 0.4656612873e-9*/
static const int IADD = 453806245;
static const int IMUL = 314159269;
static const int MASK = INT_MAX;
static const double SCALE = 1.0 / (double)INT_MAX;
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884197169399375 105820974944592307816406286
#endif
/* "Default" seed. Use your lucky number (not zero) */
static int randSeedP = 17;
void InitRand(int randSeedI)
{
if (randSeedI != 0) {
randSeedP = randSeedI;
}
else {
randSeedP = time(0);
}
}
double RandR()
{
randSeedP = (randSeedP * IMUL + IADD) & MASK;
/*printf("randSeedP = %d, randSeedP*SCALE = %f\n",
randSeedP, randSeedP*SCALE); */
return (randSeedP * SCALE);
}
#if NDIM == 2
typedef struct _VecR
{
double x;
double y;
} VecR;
void VRand(VecR * p)
{
double s;
s = 2. * M_PI * RandR();
p->x = cos(s);
p->y = sin(s);
}
#elif NDIM == 3
typedef struct s_VecR
{
double x;
double y;
double z;
} VecR;
inline static double Sqr(double r) {return r*r;}
void VRand(VecR * p)
{
double s, x, y;
s = 2.;
while (s > 1.) {
x = 2. * RandR() - 1.;
y = 2. * RandR() - 1.;
s = Sqr(x) + Sqr(y);
}
p->z = 1. - 2. * s;
s = 2. * sqrt(1. - s);
p->x = s * x;
p->y = s * y;
}
#endif
int main()
{
double x;
int i;
double sum;
int num = 100000000; /* Generate this number of samples */
double xmax, xmin;
if (sizeof(int) < 4) {
fprintf(stderr, "This program requires 32-bit ints\n");
exit(EXIT_FAILURE);
}
printf("IMUL = %d, IADD = %d, SCALE = %.16e\n\n",IMUL, IADD, SCALE);
/*
* Use any seed that you want here. The same sequence
* will be generated for every program run.
*/
InitRand(12345678);
/*
* If you want to let InitRand generate its own seed, call
* it with an argument of zero.
*/
/* InitRand(0) */
/*
* I'll just keep track of the max and minimum values
* returned by RandR and calculate the average of all
* returned values.
*/
xmax = RandR(); /* first call to RandR */
xmin = xmax;
sum = xmax;
for (i = 1; i < num; i++) {
x = RandR();
sum += x;
if (x > xmax) {
xmax = x;
}
if (x < xmin) {
xmin = x;
}
}
printf("xmin = %.16e, xmax = %.16e\n", xmin, xmax);
printf("sum = %.16e, avg = %.16e\n", sum, sum/(double)num);
return 0;
}
Output:
IMUL = 314159269, IADD = 453806245, SCALE = 4.6566128752457969e-010
xmin = 2.0489096651081506e-008, xmax = 9.9999992083758116e-001
sum = 4.9994861287474781e+007, avg = 4.9994861287474779e-001
Regards,
Dave
amirvahid
05-14-2009, 05:08 PM
Thanks Dave,
Yes I guess the main difference b/w Linux and Windows is that the typedef struct in Windows should have a name from the beginning like the one that you use:
typedef struct s_VecR {
.....
} VecR
However, in Linux they don't have the name at the beggining. They only have it at the end:
typedef struct {
...
} VecR
I have enclosed rap04 vector definition that I use in my programs. I conclude this reply with this question:
Is there any structure and macro definitions for vectors in NR3 like the one that I have attached? I want to use a reliable windows style b/c it is just easier to debug and then run my codes on Linux since we have thousands of CPU clusters. Hence, I am looking for a good standard vector and matrix library.
P.S. The website for downloading rap04 source codes is:
http://www.ph.biu.ac.il/~rapaport/mdbook/getmdsw.php
Warm wishes
Amir
davekw7x
05-15-2009, 12:31 PM
Thanks Dave,
Yes I guess the main difference b/w Linux and Windows is that the typedef struct in Windows should have a name from the beginning like the one that you use...
No. I used that style as a matter of habit. In a C program the typedefs for those particular structs, the tag (the identifier before the {} braces) is not needed. Either form is standard C (and works in C++). There is no difference between "C for Windows" and "C for Linux."
Different compilers may have different non-standard features and library functions, and certain compilers might try to adhere more closely to language standards than others, but it's not a matter of "Windows" versus "Linux." Compilers, like humans, all have their little quirks.
The differences between Windows programs and Linux programs become important when non-standard functionality, like graphics interfaces, are required. The Microsoft Windows applications programming interface is unique (non-standard C/C++ functions). It is totally different from what you might need for an X-windows based system like Linux. Although there are some cross-platform graphics interface tools and toolkits that make portability somewhat less painful, there still are "little tweaks" that are usually required to port an application from one to the other.
For non-graphics (command-line) programs we can "easily" create code that will be compilable on either kind of system.
For maximum chances of maximum portability between different compilers and operating systems, I recommend sticking to the Standards wherever possible, and carefully documenting deviations that may be necessary when standard library functions and standard language features are not adequate for a particular purpose.
The main reason that the code of your initial post wouldn't compile with your Visual C++ compiler is that it used the non-standard library function gettimeofdat() to obtain a seed that depends on system time (and, therefore would give a different sequence of random variants every time the program was run).
I just used the standard library function time() to give a different seed each time (assuming the program runs were at least a second apart). This is not an exact functional equivalent, but might be OK, depending on the application. IWFMYMMV (It Works For Me; Your Mileage May Vary).
Other than that, I added the headers and made guesses about a few typedefs that would make it compilable, and I made a few stylistic changes that are not really very important to most people. The code that I showed you will compile and give exactly the same results with any reasonably modern compiler. I compiled and executed it with compilers from Borland, Microsoft, GNU on Windows, and GNU on Linux. Results were identical.
...I am looking for a good standard vector and matrix library...There is no matrix library defined in the C or C++ language standards documents.
There is a standard vector class, but it doesn't extend beyond 1 dimension.
Since you came to the Numerical Recipes forum I thought you were going to use Numerical Recipes functions in your programs. The file nr3.h has C++ definitions for matrix and vector classes that are compatible with the various NR functions, but it does not include such features as matrix addition, multiplication, etc. They do have things like overloaded assignment operators that perform the copying of matrix and vector objects with simple assignment statements. This is, in my opinion, a great programming advance over C, where you would have to call a function to make a copy of a matrix.
The text also mentions that it is entirely possible to link in other vector and matrix libraries (that might have a more complete set of mathematical operations) in place of the NR matrix and vector classes. I won't try to recommend or dis-recommend any of the many, many libraries that you can find floating around (some are free; some are not).
However...
If I were looking for such things to use with the NR functions, I would probably start at the user contributions page on this web site: http://www.nr.com/contrib/
The website for downloading rap04 source codes is:...
The set of source files may be perfectly appropriate for teaching and illustrating certain aspects of the topic of that text. Having a set of example programs and the tools to build applications that illustrate the topic is a very valuable teaching adjunct. There is no question about that.
However...
[Dave's Personal Opinion]
The code is not safe. All of those macros and functions that use global variables---I shudder at the thought of debugging any significant application code.
The code is not suitable for teaching or learning safe and sound programming practices. You said that you wanted a "reliable" style. I don't think the style is reliable. (For the same reasons that I stated previously.)
If you ever have to ask for help from someone not immersed in the particular culture of that programming approach to MD, you may have problems getting experienced programmers to dive in.
[/Dave's Personal Opinion]
Just my opinion. Others may have reasons (even good reasons) to disagree, and I wouldn't dream of arguing any of these points.
Bottom line, and my final opinion on the subject: If you are going to use NR3 functions that require vectors and/or matrices, then, for goodness sake, write C++ and don't use C-style constructs and functions as are supplied in the MD library.
Regards,
Dave
amirvahid
05-15-2009, 02:56 PM
Thanks Dave,
Yes I guess I should completely switch to C++. However, sometimes, you have more important things to do and you don't want to reinvent the wheel but ultimately, I would agree with your opinion. In this case, I wanted to use the MD library but it gave me the following error massage:
error C2011: 'RMat' : 'struct' type redefinition
However, it was only defined once!
I have enclosed my code and I intend to use NR3 as a supply for random # and matrices thereafter but an initial help from you would regarding this error message be appreciated. Thanks
Amir
davekw7x
05-15-2009, 05:52 PM
...
I have enclosed my code and I intend to use NR3...
On your Linux system:
1. Unzip artmdsim2.tar.gz somewhere convenient.
2. Navigate to the artmdsim2 directory and create a new directory named "working" at that level.
3. Copy the following files from artmdsim2/src to artmdsim2/working:
in_mddefs.h
in_namelist.h
in_proto.h
in_vdefs.h
in_errexit.c
in_namelist.c
in_rand.c
pr_14_1.c
4. Rename the .c files to .cpp. Now the working directory has the following files:
in_mddefs.h
in_namelist.h
in_proto.h
in_vdefs.h
in_errexit.cpp
in_namelist.cpp
in_rand.cpp
pr_14_1.cpp
Now edit the last three lines of pr_14_1.cpp so that it includes the renamed cpp files. That is, the last three lines are
#include "in_rand.cpp"
#include "in_errexit.cpp"
#include "in_namelist.cpp"
The files are now set up and ready to compile. Do not make any other changes.
From the Linux command line, execute the following:
g++ pr_14_1.cpp -lm -o pr_14_1
Now you should have an executable file named "pr_14_1" It expects a data file named pr_14_1.in, so copy that file from the artmdsim2/data directory to the working directory.
Execute the pr_14_1 program.
NameList -- data
density 0.800000
eventMult 4
initUcell 8 8 8
intervalRdf 0.250000
intervalSum 5.00000
limitEventCount 1500000
limitRdf 100
rangeRdf 4.00000
sizeHistRdf 200
temperature 1.00000
----
0.00 0 0 0.000 1.499
5.00 114347 23666 -0.000 1.499
10.00 231286 46850 0.000 1.499
15.00 348145 70645 0.000 1.499
20.00 464560 93817 0.000 1.499
.
.
.
Regards,
Dave
Footnote: If you are stuck with Windows and Visual C++, then perform all of the things that I said above.
When you compile with VIsual C++ you will get some errors.
Here's what you do (assuming you have already made the changes that I enumerated above):
Put the following three lines at the top of p_14_1.cpp:
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
I would put them at the top of in_rand.cpp also, but you may not need them there.
In the file in_rand.cpp:
Comment out (or delete) the line that includes <sys/time.h>
Change the InitRand function to the following
void InitRand (int randSeedI)
{
if (randSeedI != 0) {
randSeedP = randSeedI;
}
else {
randSeedP = time(0);
}
}
Do not make any additional changes.
Try to compile it now with Visual C++
amirvahid
05-16-2009, 11:03 AM
Dear Dave,
The cpp Linux works fine for me as the C Linux was working fine. Thanks for that.
Regarding the Windows Visual C++:
I modified the code as you told me w/o any additional changes. I got the following errors:
"c:\documents and settings\hp operator\my documents\visual studio 2005\projects\rapaport\chap14\pr_14_1\pr_14_1\in_n amelist.cpp(9) : error C2065: 'FILE' : undeclared identifier"
"c:\documents and settings\hp operator\my documents\visual studio 2005\projects\rapaport\chap14\pr_14_1\pr_14_1\in_n amelist.cpp(14) : error C2065: 'nameList' : undeclared identifier"
Hence, I decided to add the following to the in_namelist.cpp:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "in_namelist.h"
I also put the following in "in_namelist.h":
VecR region, vSum;
VecI cellRange[2], cells, initUcell;
real collCount, crossCount, density, intervalSum, nextSumTime,
kinEnVal, temperature, timeNow, velMag;
int *cellList, eventCount, eventMult, evIdA, evIdB, limitEventCount, moreCycles,
nMol, poolSize;
real *histRdf, intervalRdf, nextRdfTime, rangeRdf;
int countRdf, limitRdf, sizeHistRdf;
NameList nameList[] = {
NameR (density),
NameI (eventMult),
NameI (initUcell),
NameR (intervalRdf),
NameR (intervalSum),
NameI (limitEventCount),
NameI (limitRdf),
NameR (rangeRdf),
NameI (sizeHistRdf),
NameR (temperature),
};
And commented them out from pr_14_1.cpp.
However, I again get the redefinition errors that I mentioned before. I have enclosed all of the source codes that I modified. I somehow lost and don't understand why a code that is working fine in Linux cannot work fine in Visual C++. I would appreciate your help in compiling this project in Visual C++ and after that I would have a basis for debugging Linux codes on Visual C++.
Warm regards,
Amir
davekw7x
05-16-2009, 12:04 PM
...Regarding the Windows Visual C++:
I modified the code as you told me w/o any additional changes. I got the following errors:
If you are compiling from the Integrated Development Environment, the only file in the project should be pr_14_1.cpp This set of programs can be compiled "as-is" (or with the minimal changes that I suggested) only if you compile it this way. See Footnote.
I strongly recommend compiling from a Windows command line.
cl pr_14_1.cpp /EHsc
When people compile from within the Integrated development Environment, it is very difficult to help them debug by remote control, since we have no way of knowing (and it is practically impossible for people to tell us in a way that we can understand) the IDE settings.
I respectfully suggest the following:
Go back to my original instructions. Start again with the original files and make the modifications that I showed. Don't make any other changes. Try compiling from the command line. If you simply must compile from within the IDE, make sure that the only file in the project is pr_14_1.cpp
Regards,
Dave
Footnote: This is one of the reasons that I expressed my opinion that use of the MD package is not a very effective way to teach programming (or to learn). The conventions used are simply not the "right" way (again, it's just my opinion based on the way that I feel that "most" people do things, and certainly the way that I feel that "most" experienced programmers do things. Including other multiple source in the main program can work, but in order to have a chance of getting things to work, you can't branch out on your own unless you take the time and expend the effort to learn some "stuff" about how it really works.
amirvahid
05-16-2009, 12:57 PM
I tried to compile from the command line but it gives me these errors:
C:\pr_14_1>cl pr_14_1.cpp /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
pr_14_1.cpp
c:\pr_14_1\in_namelist.h(2) : error C2365: 'N_I' : redefinition; previous defini
tion was 'enumerator'
c:\pr_14_1\in_namelist.h(2) : see declaration of 'N_I'
c:\pr_14_1\in_namelist.h(2) : error C2365: 'N_R' : redefinition; previous defini
tion was 'enumerator'
c:\pr_14_1\in_namelist.h(2) : see declaration of 'N_R'
c:\pr_14_1\in_namelist.h(2) : error C2371: 'VType' : redefinition; different bas
ic types
c:\pr_14_1\in_namelist.h(2) : see declaration of 'VType'
c:\pr_14_1\in_namelist.h(12) : error C2371: 'NameList' : redefinition; different
basic types
c:\pr_14_1\in_namelist.h(12) : see declaration of 'NameList'
c:\pr_14_1\in_namelist.h(21) : error C2371: 'ValList' : redefinition; different
basic types
c:\pr_14_1\in_namelist.h(21) : see declaration of 'ValList'
pr_14_1.cpp(58) : error C2065: 'moreCycles' : undeclared identifier
pr_14_1.cpp(59) : error C2065: 'eventCount' : undeclared identifier
pr_14_1.cpp(63) : error C2065: 'limitEventCount' : undeclared identifier
pr_14_1.cpp(73) : error C2065: 'timeNow' : undeclared identifier
pr_14_1.cpp(73) : error C2065: 'nextSumTime' : undeclared identifier
....
.....
Also debugging from the IDE and having only pr_14_1 in the project does not work properly. Although it doesn't give any errors (it has some warnings) but when I want to execute the file, it generates this message:
Debug Error!
Run-Time check failure#2-Stack around the variable 'buff' was corrupted. (Please retry to debug the application)
I agree with you that I should start writing the styles as NR3 b/c it is much more easier and reliable. However, first I should be able to debug this simple DMD example on IDE since I am writing the NR3 on IDE and IDE has better debug facility.
Thanks for your helps.
Best wishes
davekw7x
05-16-2009, 01:59 PM
I tried to compile...
Unzip the attachment. From 0_readme.dave:
Tested on Windows XP with
Microsoft Visual C++ Express 2005
Microsoft Visual C++ Express 2008
cygwin/GNU g++ versions 3.4.4 and 4.3.2
Linux/GNU g++ version 4.1.2
With Visual Studio C++ Express 2005 here's the results of the compile command:
================================================== ====
F:\home\dave\cprogs\artmdsim2\forum>cl -wd4996 pr_14_1.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
pr_14_1.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
/out:pr_14_1.exe
pr_14_1.obj
================================================== ====
Here's the beginning of a run:
================================================== ====
F:\home\dave\cprogs\artmdsim2\forum>pr_14_1
NameList -- data
density 0.800000
eventMult 4
initUcell 8 8 8
intervalRdf 0.250000
intervalSum 5.00000
limitEventCount 1500000
limitRdf 100
rangeRdf 4.00000
sizeHistRdf 200
temperature 1.00000
----
0.00 0 0 0.000 1.499
5.00 113943 23337 0.000 1.499
10.00 231605 46541 -0.000 1.499
================================================== ====
If you have a different compiler of (a different version of compiler), I can't help you any more. The code is just too painful.
Sorry.
Regards,
Dave
amirvahid
05-16-2009, 04:48 PM
Thanks Dave. I'll try to write my programs based on your & nr3 style. I could compile the program this time on command line but on ide it has the previous debug error. The command line is pretty much similar to linux. However, working with debug mode is much more efficient with ide. I am writing the code based on your style. Thanks for your helps.
amirvahid
09-08-2010, 11:13 PM
Just a quick reminder:
:D The command for debugging, i.e., " KDbg in Fedora and gdb", would be:
g++ pr_14_1.cpp -lm -o pr_14_1 -g