|
How about something like:
::srand(::time(NULL));
int min = 10;
int max = 20;
for (int i = 0; i < 100; i++)
{
int n = min + (::rand() % (max - min + 1));
TRACE(_T("n = %d\n"), n);
}
Or something similar?
When I am king, you will be first against the wall.
|
|
|
|
|
Robert,
Makes sense but, I am not familiar with the TRACE
could you explain this a bit more?
TRACE(_T("n = %d\n"), n);}
Thanks,
sj
|
|
|
|
|
It's part of MFC. It outputs to the debug window when in Debug mode, and does nothing in Release mode.
|
|
|
|
|
It works great thanks,
sj
|
|
|
|
|
Not to pick nits or anything, but I believe that the randomness of rand() is not guaranteed if you do a mod. You are essentially only using a subset of the random bits. If you want to be proper, you should divide the result by RAND_MAX to get a float in [0.0,1.0) which you can then scale apropriately.
J
"You can get anything you want at Alice's Restaurant."
|
|
|
|
|
When I divide by RAND_MAX, I get a 0 as the value.
When I divided by the mod I get a number between my min and max value, but it was a int.
How can I get the rand() to return a value between 2 doubles.
Here is my code, it might give you a better idea.
thanks,
sj
<br />
double createRandNum(TSimSignal &s, double min, double max){<br />
<br />
double n; <br />
<br />
for(int i = 0; i < 100; i++){ <br />
n = min + ((double)rand()) % (max - min + 1.0));<br />
or<br />
n = min + ((double)rand()/RAND-MAX) % ((double)(max - min + 1))); <br />
}<br />
cout << endl << "The random number is : " << rand()<< " N is : " << n << "\n";<br />
return n;<br />
}<br />
<br />
|
|
|
|
|
int randInRange(int minVal, int maxVal)
{
double scale = rand() / static_cast<double>(RAND_MAX);
return static_cast<int>(minVal + scale * (maxVal - minVal));
}
This returns a value in the range [minVal, maxVal). If you need [minVal, maxVal] you'll have to add 1 to the (maxVal - minVal) in the last line.
J
"You can get anything you want at Alice's Restaurant."
|
|
|
|
|
Does anyone know if it is possible to find the creation date\time of an executable from within the file ?
thanks in advance!
|
|
|
|
|
You could try fetching the full path of the EXE using ::GetModuleFileName and then use CFileStatus::GetStatus(...) to get the creation date...
When I am king, you will be first against the wall.
|
|
|
|
|
Hello guys. I'm new in this place and I'm new in the VC++ programming world (although I have tackled on C++ before). Well the thing is that we have 2 CCD cameras, specifically 2 EDCN2000N Cameras from Electrim Corp. and everything is fine and cool but ... they gave us a software that allows us to view the output of 1 camera and change some options, etc. but the most important thing is that it has a Save function (to save the image).
Now, the thing is that we want to have output from the 2 cameras (since we have 2 connected to the same interface card.) We contacted them and they sent us the source code for the new software and it works flawlessly but ... IT DOESN'T HAVE A SAVE FUNCTION!!!!! (you can look at the code in a zip file here: http://www.prtc.net/~eagle1/soft.zip ).
Now, I don't know much about VC++ so I sent them another email and they sent me a code that has a save function (to save to tiff) but again, it works for one camera at the time and I have tried like to copy and paste the code to save as tiff file to the program with multiple camera support but it does nothing (http://www.prtc.net/~eagle1/readedcn.zip) .
So question is, is there an easy way to save those images using the code from the soft.zip and readedcn.zip I posted?
I was also looking at CxImage (or something similar) but ... it seems difficult to use! I made changes in the .rc file and added a Save button and in the sdk_mh.c file I added a case for IDM_Save but I just put a message box to know it was working but,
HOW do I Save the Window contents (i.e. the image) to a Tiff file or jpeg? Can you help me with this?
Thanks in advance!!
|
|
|
|
|
|
when I call as :
CFileFind localFind;
localFind.FindFile (_T("*.*"));
localFind.FindNextFile ();
FILETIME timeLastWrite;
localFind.GetLastWriteTime (&timeLocalFile);
SYSTEMTIME timeFile;
FileTimeToSystemTime(&timeLastWrite,&timeFile);
i found that the file'w lastwritetime I got is inconsistent with the info. displayed by win2k.
win2k: 2003-06-25 23:50
programme: 2002-06-25 23:50
wrong with Year?
thx for advance
|
|
|
|
|
Does this happen with all files?
What happens if you use this instead:
CTime timeLastWrite;
localFind.GetLastWriteTime(timeLastWrite);
TRACE("%d\n", timeLastWrite.GetYear());
|
|
|
|
|
Hello you out there,
I am currently developing a tiny database program and therefore using a CMapStringToOb to store my user-defined objects in memory. During development, I suddenly faced a strange problem concerning memory usage and memory leaks. I thought that something was wrong with my class and therefore created a small class called CTest, which in fact is only a "box" or an int, I know it's silly, but, hey, it's at least maybe more easy to understand than a class with CStrings and so on...
<br />
class CTest : public CObject<br />
{<br />
private:<br />
int m_TestInt;<br />
<br />
public:<br />
CTest() { m_TestInt = 0; }<br />
CTest(int NewTestInt) { m_TestInt = NewTestInt; }<br />
~CTest() {}<br />
void SetInt(int NewTestInt) { m_TestInt = NewTestInt; }<br />
int GetInt() const { return m_TestInt; }<br />
};<br />
Then I'm using it with my CMapStringToOb:
<br />
CMapStringToOb *pTestObs = new CMapStringToOb;
pTestObs->InitHashTable(50000,TRUE);
<br />
char buffer[6];<br />
<br />
for(int i = i; i<= 49999; i++)<br />
{<br />
pTestObs->SetAt(_itoa(i,Buffer,10), new CTest(i));<br />
}<br />
Ok, up to here, everything works fine. I can retrieve my elements by calling "Lookup" and so on.
But now, please have a look at that piece of code in which I'm trying to delete my objects as well as the pointers and the "Map" as stated in the MSDN:
<br />
POSITION pos = pTestObs->GetStartPosition();
<br />
while(pos != NULL)<br />
{<br />
CString String;<br />
CTest* pTest = NULL;<br />
pTestObs->GetNextAssoc(pos, String, (CObject*&)pTest);<br />
pTestObs->RemoveKey(String);<br />
delete pTest;<br />
}<br />
<br />
<br />
delete pTestObs;<br />
The problem with this piece of code now is that, in Debug mode, everything seems to work just fine, I added some ::AfxMessageBox(...); statements in order to know what goes right and what goes wrong.
These CObjects need roughly 7 MB of RAM on my machine. In Debug mode, the upper code frees 7 MB, as previously needed and allocated. But in Release mode, after creating these objects and inserting them into the CMapStringToOb, this part of the code only frees round 3 MB of the memory. But... where is the rest?! I am leaking 4 MB of memory and dealing with much larger objects in the REAL program, which might easily mean around 20 MBytes.
Can any one help me? Does anyone know what I am doing wrong?
Best regards and thanks in advance,
pf7
(@ All Americans: I live in Germany, so please do not think that I won't reply immediately. It's just because of the difference in time. I'll reply!)
|
|
|
|
|
No one to help me out there?
|
|
|
|
|
Get rid of the RemoveKey statement. After deleting all the objects, you can call RemoveAll().
onwards and upwards...
|
|
|
|
|
Thanks, but, as you can see, I've already commented the "RemoveAll()" out with the statement "didn't help either!" The fact is that, as stated in my first question, it seems as if the objects are not deconstructed correctly. But only in Release mode, not in Debug mode. I've heared that the debug mode features some type of "garbage collection" but when executing my script I do not get compiler warnings or debugger messages saying that it is deleting / destructing any instances of my object.
It's strange, I know, but if any one of you tried it out the way I did, it would - hopefully - have the same result: a memory leak of which I don't know what the cause might be.
|
|
|
|
|
@basementman: Thanks for at least reading the whole thing Seems as if no one except you has any tips or wants to help me finding the solution
|
|
|
|
|
Are you still having this problem. I have used code like this a hundred times in the past. I avoid MFC template classes and I recommend STL now because after learning STL you will get template classes with much more flexibility, functionality and portability. However looking at your code I do not see any reason why there is a memory leak.
John
|
|
|
|
|
Well, at the moment I'm just a beginner that tries this MFC collection class. Sadly, I've got no experience in STL at the moment. Maybe I'll learn how to use it by the time...
And yes, I've still got the same problem. I really do not know what causes these problems. As I before all this used a much more complex class with some CString(s) and COleDateTime, I thought that I did something wrong when deleting the whole object (maybe something in the destructor), but I get this problem even in this simple CTest class. I also before this created the whole thing on the stack CMapStingToOb Test; and thought it was this that caused the problem but it remained when I created it on the heap CMapStringToOb *pMTO = new CMapStringToOb; ... Do you know if it has something to do with new and the associated pointers to the object instances or the instances of my CMapStringToOb?
I'll also ask a friend of mine who's got a Win XP machine to test it (I've got 98SE), maybe it is just the system... For what reason ever.
|
|
|
|
|
I did not forget about this problem however I am too busy to help at the moment I look at as soon as I can.
John
|
|
|
|
|
First of all I would like to thank you; first of all you should do what you have to do
It's just a private project (a program I want to be used by my school) and I've got time to wait as there is no need for the program to be finished within a certain time. I'm just programming so that I get more versed in the whole subject of computer, programming, databases and data processing.
I would be glad if you had a look on it once again, but you don't have to as there really is no
need for you to spend your free time on this, you surely have got something better to do
Thanks again first of all (and in advance )!
|
|
|
|
|
I know we can use Class Wizard to set maximum length of a CString in an edit box. Is there any way to change the maximum length of an edit box without using the Class Wizard?
|
|
|
|
|
CEdit::SetLimitText or something similar (or send an EM_SETLIMITTEXT message).
When I am king, you will be first against the wall.
|
|
|
|
|
Thanks Rob, it works perfectly!
|
|
|
|