|
Just check this.
int rand1( int nData)
{
return rand() % 100 + nData;
}
int main(int argc, char *argv[])
{
int Temp;
srand(time(NULL));
for( int i = 0; i< 11; i++)
{
Temp = rand1(1) ;
cout<<"Num1=="<<temp<<endl;
temp="rand1(2);
" cout<<"num2="="<<Temp<<endl;
" cout<<"num3="="<<Temp<<endl;
" }
="" return="" 0;
}
<div="" class="signature">Величие не Бога может быть недооценена.
|
|
|
|
|
Thanks.
I am using a different random number generator which generates a normal distribution of a random number generator - N(0,1). This function is from a book called: Numerical Recipes.
When I call it, I call gasdev(int &idum); For example: t=time[i]+sd_error*gasdev(int &idum);
In one subroutine, I need to call gasdev(int &idum) three times, but I want each time it is independent.
int idum1=-1;
int idum2=-1;
int idum3=-1;
//
date=time[i]+t_sd_error*gasdev(idum1);
money=t_sd_error*gasdev(idum2);
force=force_sd_error*gasdev(idum3);
date, money, and force are totally independent variables. Do you think I am OK to make the three calls independent with above idum1, idum2, and idum3 ?
Here is it:
//
double C_procDlg::ran1(int &idum)
{
const int IA=16807,IM=2147483647,IQ=127773,IR=2836,NTAB=32;
const int NDIV=(1+(IM-1)/NTAB);
const double EPS=3.0e-16,AM=1.0/IM,RNMX=(1.0-EPS);
static int iy=0;
static int iv[100];
int j,k;
double temp;
if (idum<=0||!iy) { //initialize
if(-idum<1) idum=1;
else idum=-idum;
for(j=NTAB+7;j>=0;j--){
k=idum/IQ;
idum=IA*(idum-k*IQ)-IR*k;
if(idum<0) idum+=IM;
if(j<NTAB) iv[j]=idum;
//
}
iy=iv[0];
}
k=idum/IQ;
idum=IA*(idum-k*IQ)-IR*k;
if (idum<0) idum+=IM;
j=iy/NDIV;
iy=iv[j];
iv[j]=idum;
if((temp=AM*iy)>RNMX) return RNMX;
else return temp;
}
//
double CBlast_vib_procDlg::gasdev(int &idum)
{
static int iset=0;
static double gset;
double fac,rsq,v1,v2;
//
if(idum<0) iset=0;
if (iset==0){
do{
v1=2.0*ran1(idum)-1.0;
v2=2.0*ran1(idum)-1.0;
//
rsq=v1*v1+v2*v2;
}
while(rsq>=1.0||rsq==0.0);
fac=sqrt(-2.0*log(rsq)/rsq);
gset=v1*fac;
iset=1;
return v2*fac;
}
else{iset=0;
return gset;
}}
modified on Saturday, April 10, 2010 1:58 AM
|
|
|
|
|
What do you think if I use your above code to generate three seeds for my three random number generators?
My aim is to generate three random number sequence which are totally un-correlated each time to run the program?
Thanks
|
|
|
|
|
I believe this logic surely generate new sequences, but remember that
rand() % X + nData;
If the value si X is bigger then there is less chances that the same number is repeated.
Величие не Бога может быть недооценена.
|
|
|
|
|
Thanks. I like you to do more comments. What you suggest ?
|
|
|
|
|
Thanks. Your above code works. I will use it to generate seeds for a generator of a normal distribution diviates
|
|
|
|
|
Hi, I created a thread in CView::OnInitialUpdate method of my CView class now I want to know how could I post my mouse move, wheel, RClick messages, (captured by CView) to the started thread, I have saved my thread pointer in m_pThread datamember.
a simple code would make the case more clear to me,
Regards
|
|
|
|
|
|
Thank you,
But what do I put to the the third and 4th arguments?
BOOL CMyView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
::PostThreadMessage(m_pThread->m_nThreadID, WM_MOUSEWHEEL, ???, ???);
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
|
|
|
|
|
Use MAKELPARAM and MAKEWPARAM to set the delta and point. as the next parameters.
Величие не Бога может быть недооценена.
|
|
|
|
|
Fellow coders,
RE: a C++ design problem.
I am attempting to construct a C++ class hierarchy on top of a C legacy system. The result
will be then placed into an embedded controller. The controller communicates with a Windows
program via a series of 'software pipes' to a supplied DLL and then calls in the Windows
program to fetch information from the DLL. The data in a given pipe will be of a specific type.
In order to generalize this process the third party library uses the following header:
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef union gen_scalar {
double _double;
__int64 _i64;
float _float;
long _i32;
short _i16;
DWORD _long;
WORD _word;
BYTE _byte;
} GENERIC_SCALAR;
And then, for example, reading a long from a pipe would be:
long Get(PIPE* pipe)
{
GENERIC_SCALAR v;
pipe_value_get(pipe, &v);
Return v._i32;
};
So, the hierarchy starts with a base class:
class Pipe
{
public:
Pipe(PIPE* pipe)
: pipe_(pipe)
{};
virtual ~Pipe()
{};
protected:
PIPE* pipe_;
};
where a PIPE* is just a handle to the communication pipe.
Then an input class is needed:
class IPipe : public Pipe
{
public:
IPipe(PIPE* pipe)
: Pipe(pipe)
{
pipe_open(pipe_, P_READ);
};
};
There would also be a corresponding output class OPipe.
And now the part I need help with!
Suppose I know a pipe will contain a series of 'shorts'. Then I could create:
class ShortIPipe : public IPipe
{
public:
ShortIPipe(IPIPE* pipe)
: IPipe(pipe)
{};
short Get() const
{
GENERIC_SCALAR v;
pipe_value_get(pipe_, &v);
return v._i16;
};
};
And so on for a long, double, float, etc, pipe(s).
However, the only difference between all these classes is the return value, that is,
v._i16 or v._i32, etc.
I'd prefer to create a single class which can handle them all. Maybe a template class?
For example:
template <typename T>
class IPipe : public Pipe
{
public:
IPipe(PIPE* pipe)
: Pipe(pipe)
{
pipe_open(pipe_, P_READ);
};
T Get() const
{
GENERIC_SCALAR v;
pipe_value_get(pipe_, &v);
return v._???;
};
};
Used like:
IPipe<short> spipe(...);
short s = spipe.Get();
And so, (finally) the question is how, within the Get() member, do I define the return value?
Note: the embedded controller cannot support RTTI. And I'd prefer not to have a big switch.
Any help would be much appreciated!
Kylur.
|
|
|
|
|
How about Template specialization?
template<> class IPipe<short>
{
...
return v._i16;
...
}
template class IPipe<long>
{
...
return v._i32;
....
}
|
|
|
|
|
Thanks for the reply, Jonathan.
Yes, this works - if fact it was my original attempt! I rejected it only because it just looked too wordy - I know, bad reason but I've got a little extra time to do investigations on this project so I thought I'd try a few other ideas.
Regards,
Kylur.
|
|
|
|
|
template<typename T> T
get_the_value(gen_scalar const& gs) const;
template<double> inline double
get_the_value(gen_scalar const& gs) const { return gs._double; }
template<short> short inline
get_the_value(gen_scalar const& gs) const { return gs._i16; }
template<typename T>
class IPipe : public Pipe
{
public:
IPipe(PIPE* pipe) : Pipe(pipe) { pipe_open(pipe_, P_READ); };
T Get() const
{
GENERIC_SCALAR v;
pipe_value_get(pipe_, &v);
return get_the_value<T>(v);
};
};
|
|
|
|
|
Thank you for the reply, Berndus.
This answer really caught my eye as it 1) took advantage on templates and specialization, 2) the helper functions can all be moved into the IPipe class as private members, 3) is understandable and 4) is short.
Looking closer at the get_the_value functions and they start to look very similar to the reinterpret_cast as suggested in another reply - the only difference being explicitly returning the union member.
Good job! I think this is the solution I'll use.
Regards,
Kylur.
|
|
|
|
|
Did you try: return (T&)v;
...cmk
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
|
|
|
|
|
Thanks for the reply, "cmk".
And no I had not tried your suggestion.
But now I (almost) have (I used return reinterpret_cast<t&>(v);) and yes it does work! And I'm not sure if you're a friggin' genius or if I'm just an idiot for not seeing this solution.
For some unknown reason however, I'm a little reluctant to use this - "gurus" tend to frown on casts and in this case I'm not really sure what a reinterpret_cast of a union is really doing (although I have to say it does seem to work).
I'll really have to think about this some more!
Regards,
Kylur.
|
|
|
|
|
20 yrs of C++, more for C.
There is NOTHING wrong with using the (T&) cast.
...cmk
The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.
- John Carmack
|
|
|
|
|
|
|
Hi,
if yes then thread id's are unique
as only the thread id is specfied on the PostThreadMessage
MSDN says that for msgs WM_USER and above sent to another process I must do custom marshalling
Don't know what this means ???
|
|
|
|
|
As far as i know, yes, PostThreadMessage does work between processes, and marshalling -also as far as i know- is executing code in the context of another thread, something like, if you have thread A and B and the method C and the thread A needs the method C to be executed by thread B then A "marshalls" the call to thread B (by sending messages, or using events...), there's probably much more to it but it basicly means this. Correct me anyone if i am completely wrong with this...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
And Can I also use wparam and lparam
to send data to that thread/process
|
|
|
|
|
That depends on the kind of data you wish to send. For example sending a simple integer to the thread is ok, but for example sending a pointer to a string (converted to LPARAM or WPARAM) and trying to use it on the other end will most likely lead to crashes.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Please check the below, it will help correct ur understanding.
Величие не Бога может быть недооценена.
|
|
|
|