|
I get the same number each time.
When I try to cast the rand() function to a double I always get a 0.
I'm about to give up.
I sould probably spend my time writing a class that generates floats or doubles.
It seems like all the random generating classes only generate ints.
thanks for the try,
sj
|
|
|
|
|
Did you set the seed for rand()?
srand( (unsigned)time( NULL ) );
does this pretty well.
--
Ian Darling
|
|
|
|
|
johnstonsk wrote:
When I try to cast the rand() function to a double I always get a 0.
Which indicates something is off kilter. Mixing int and double in this fashion is not likely to produce the desired effect. I suggest rolling your own pseudo random generator.
A simple algorithm looks like:
static unsigned long ulSeed = 1;
#define A 48271L
#define M 2147483647L
double Random( void )
{
ulSeed = (A * ulSeed) % M;
return (double) ulSeed / M;
}
void Init( unsigned long ulVal )
{
ulSeed = ulVal;
}
Note that this could overflow. A fix by L. Schrage for 32-bit machines adds:
#define Q (M / A)
#define R (M % A)
double Random( void )
{
long lTempSeed;
lTempSeed = A * (ulSeed % Q) - R * (ulSeed / Q);
if (lTempSeed >= 0)
ulSeed = lTempSeed;
else
ulSeed = lTempSeed + M;
return (double) ulSeed / M;
}
|
|
|
|
|
Hmmm. That worked perfectly for me. This is the entire program I used to test it:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned)time(NULL));
double max = 100.0f;
double min = 0.0f;
for(int i=0; i<20; i++)
{
char buf[128];
sprintf(buf, "%lf", ((double)rand() / RAND_MAX) * (max - min) + min);
MessageBox(NULL, buf, "test", MB_ICONINFORMATION | MB_OK);
}
return 0;
}
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
You are correct, it works great.
cheers,
sj
|
|
|
|
|
johnstonsk wrote:
it works great
Good
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Err...
while(count < 100){
printf("%d\nRandomNum is: ", randomNum);
printf("%d\nP is: ", p);
printf("%d\nD is: ", d);
count++;
}
This part of your sample code does nothing other than printing 100 times the same number 'randomNum'... you know that or?
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
Are there standard small (16x15) error and warning images that suitable for use in the report view of a list control. I have tried using the IDI_WARNING and IDI_ERROR resources but you end up with an image that looks like it has been resized from 24x24. The event viewer uses some so I guess they are available to any program once you know how to get at them!
Steve.
Systems AXIS Ltd - Software for Business ...
|
|
|
|
|
There is always the sneaky way... open event viewer, press Alt+PrintScreen, open Paint, paste in the screen and copy the desired images and paste them into your bitmap resources in your project.
onwards and upwards...
|
|
|
|
|
|
basementman wrote:
the sneaky way
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Heylo. i've got myself a vector of strings, and i want the contents of the vector to end up in a combobox. here's my for statement:
for(int i = 0; i < values.size(); i++)
{
input_tq.AddString(values.at(i));
}
where input_tq is a combo box derived from CComboBox and values is a vector of strings.
this gives me the error that it cant convers my vector to LPCTSTR. (i assume this means its expecting a sting in quotes, not a vector?)
so, how can i fix this? i cant covnert the contents of the vector to individual strings because users can add to/delete from the vector.
|
|
|
|
|
this works for me.
AddString adds a string to the combo box, so passing individual strings in the vector is ok.
when the user adds and remove strings in the vector, you must also add/remove strings from the combobox as well.
Maximilien Lincourt
For success one must aquire one's self
|
|
|
|
|
you mean you got that for loop to work? Here's the error statement i get:
error C2664: 'CComboBox::AddString' : cannot convert parameter 1 from 'std::allocator<_Ty>::value_type' to 'LPCTSTR'
with
[
_Ty=std::string
]
vector<string> values;
is how im defining my vector, im passing that into a function that reads into from a text file and passes it back by reference.
i replaced 'vector.at(i)' with "hello" and that works fine. it just doesnt like my vector of strings.
how does yours work and mine doesn't.
|
|
|
|
|
this is what I did :
...
std::vector<CString> stringVector;
stringVector.push_back("allo");
stringVector.push_back("bonjour");
stringVector.push_back("salut");
stringVector.push_back("bye");
for(int i = 0; i < stringVector.size(); i++)
{
m_ComboBox.AddString(stringVector.at(i));
}
...
if using std::string you get the string with std::string::c_str()
...
std::vector<std::string> stringVector;
stringVector.push_back(std::string("allo"));
stringVector.push_back(std::string("bonjour"));
stringVector.push_back(std::string("salut"));
stringVector.push_back(std::string("bye"));
for(int i = 0; i < stringVector.size(); i++)
{
m_ComboBox.AddString(stringVector.at(i).c_str());
}
...
Maximilien Lincourt
For success one must aquire one's self
|
|
|
|
|
excellent, my strings weren't null terminated. heh, rinky dinky c++ code...
thanks a bunch!
|
|
|
|
|
Try that:
<br />
CString tmp;<br />
for(int i = 0; i < values.size(); i++)<br />
{<br />
tmp=(CString)values.at(i);<br />
input_tq.AddString(tmp);<br />
}<br />
|
|
|
|
|
Hello everyone...
i have a very good query.
i am writing a wizard.
there is a bitmap as usual on first page and the small bitmaps on the next pages..
The main problem is that the bitmap are seen very bad when the setting of monitor is to 256 colors.
some how i have managed to show the first bitmap(watermark) clear i mean it is working fine i can atleast see the color.But in case of the bitmap (Header)in the remainig pages i am not able to color it it looks completely black and white. i have drawn the bitmap in 256 colors so i know that the color of the bitmap should be seen fine on 256.
i have tried various ways to color the header but i am not able to do it..
any one having a knowledge can please help me out.
suggestion are also welcome..
i hope u get my problem if u dont understand u can again ask me for details
please help me out its urgent..
thanx in advance
Thanx
TAKE CARE
|
|
|
|
|
hello,
i have a problem .... a can not free memory ....
i have here 3 pointers iBuffer, jByffer, pDib
at the end of the function I want to free iBuffer and pDib
and it's get me an error when I run the program ... if I remove the "free"
lines it works fine but is not right....
what can I do ??? please if anyone can help .... thank you !
void* DDBToDIB(HBITMAP _desktopBmp, DWORD *bmpsize)
{
BYTE *iBuffer, *iBufferTmp;
int iWidth, iHeight;
void *jBuffer;
int jSize;
BITMAPINFOHEADER head;
void *pDib;
BITMAP bm;
GetObject(_desktopBmp, sizeof(BITMAP), (LPSTR) &bm);
iHeight = bm.bmHeight;
iWidth = bm.bmWidth;
DWORD dwEffWidth = ((((bm.bmBitsPixel * bm.bmWidth) + 31) / 32) * 4);
if (bm.bmBitsPixel == 8) head.biClrUsed = 256;
else head.biClrUsed = 0;
head.biSize = sizeof(BITMAPINFOHEADER);
head.biWidth = bm.bmWidth;
head.biHeight = bm.bmHeight;
head.biPlanes = 1;
head.biBitCount = (WORD)bm.bmBitsPixel;
head.biCompression = BI_RGB;
head.biSizeImage = dwEffWidth * bm.bmHeight;
head.biClrImportant = 0;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;
long mypalsize = (head.biClrUsed * sizeof(RGBQUAD));
long mysize = head.biSize + head.biSizeImage + mypalsize;
pDib = malloc(mysize);
BITMAPINFOHEADER* lpbi;
lpbi = (BITMAPINFOHEADER*)(pDib);
*lpbi = head;
iBufferTmp = ((BYTE*)pDib + *(DWORD*)pDib + mypalsize);
HDC dc = GetDC(NULL);
GetDIBits(dc, _desktopBmp, 0, iHeight, iBufferTmp, (LPBITMAPINFO)pDib, DIB_RGB_COLORS);
ReleaseDC(NULL, dc);
iBuffer = (BYTE *)malloc(iHeight * iWidth * 3);
for (int c = iHeight - 1; c >= 0; c--) {
memcpy((BYTE *)iBuffer + ((iHeight - 1 - c) * iWidth * 3),
(BYTE *)iBufferTmp + (c * iWidth * 3), iWidth * 3);
}
for (c = 0; c < iHeight * iWidth; c++) {
iBufferTmp[c * 3] = iBuffer[c * 3 + 2];
iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1];
iBufferTmp[c * 3 + 2] = iBuffer[c * 3];
}
jBuffer = (BYTE*)Compress(iBufferTmp, iWidth, iHeight, 3, &jSize, 100);
FILE *dest = fopen("sent.jpg", "wb");
fwrite(jBuffer, jSize, 1, dest);
fclose(dest);
// free(pDib);
// free(iBuffer);
*bmpsize = jSize;
return jBuffer;
}
|
|
|
|
|
_crs_ wrote:
it's get me an error when I run the program
Such as?
_crs_ wrote:
pDib = malloc(mysize);
iBuffer = (BYTE *)malloc(iHeight * iWidth * 3);
Here, pDib and iBuffer are not being checked against NULL. While malloc() is probably not failing, it would behoove you to step through the code and see at what point either of these variables is being stepped on, since an invalid pointer passed to free() can cause errors.
|
|
|
|
|
the error is "DAMAGE: after normal block (#xx)"
and a made all pointers NULL befor malloc and the same ...
however I resolved with pDib by
lpbi = NULL; and now it works
but for iBuffer don't !
if I remove the following lines
/* for (c = 0; c < iHeight * iWidth; c++) {
iBufferTmp[c * 3] = iBuffer[c * 3 + 2];
iBufferTmp[c * 3 + 1] = iBuffer[c * 3 + 1];
iBufferTmp[c * 3 + 2] = iBuffer[c * 3];
}*/
everything works but ... I need them .....
|
|
|
|
|
If you attempt to free a block of memory after overwriting the end of its allocation, you will get the "DAMAGE: after normal block (#xx)" error.
|
|
|
|
|
Hi there,
I am examining some VC++ code that attempts to read asynchronous serial data via third party serial interface cards, namely (Moxa<4-port> and Aurora<8-port>).
When I set the baudrate to 19200 and above, then the data packets read are all fine and consistent.
But when I set the baudrate to 9600 and below then the problems start. The length and the content of the packets read are not always the same.
A packet consists of 44 bytes, with 8 bits, one start and stop bit only, no parity.
I have inserted the HasOverlappedIOCompleted() function before ReadFile() function, but to no avail.
Any help/advise would be appreciated.
Thanks
Louis
|
|
|
|
|
Try to read only 1 byte in ReadFile
|
|
|
|
|
Hi I'm trying to write a class for serial comms. One of the things I want to do is to be able to set the config of the serial port using the CommConfigDialog API call, but I keep getting an exception in serialui.dll. I think I need to call GetCommConfig first to get the pointer to the structure, but I can't work out how to get the size of the buffer (3rd parameter). Does anyone know how to do this? And also which buffer does it mean?
BOOL GetCommConfig(
HANDLE hCommDev,
LPCOMMCONFIG lpCC,
LPDWORD lpdwSize
);
Thanks
Andy
|
|
|
|