|
Hi,
I could come up with an infinite number of pseudo-random-number-generators; they all are periodic, i.e. after N calls they repeat the sequence; you would hope N to be large.
The simple ones have a single state variable (next in your snippet) that is operated upon (e.g. with multiply and add; or with shift and exor) and result in a period of at most 2^B for a state variable holding B bits.
I can't tell you which one is used in the standard libraries, maybe because that is super secret too.
I suggest you Google rand.c to get a small collection.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
The reason I said it is super secret is so that when the CIA/FBI/NSA kick down your door to arrest you as one of my accomplices, you can have deniability.
I thought that there was a simple random function that most programmers, that are too lazy to make their own, use. Perhaps you know of a method to predict the next number of a randomizer function. This is why I wanted to know the definition of the rand() function so I can write a program to predict the next 'random' number in a series.
but thanks anyway...
by the way, after this conversation, your phone might be tapped...
KOM UIT DAAAAA!!!
|
|
|
|
|
Grimes wrote: I am looking for the rand() function definition
You failed to mention what compiler you are using. Your in luck, I have several installed.
Visual Studio 6:
C:\Program Files (x86)\Microsoft Visual Studio\VC98\CRT\SRC\RAND.C
Visual Studio 2005:
C:\Program Files (x86)\Microsoft Visual Studio 8\VC\crt\src\rand.c
Visual Studio 2008:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\rand.c
You could also take a look at the rand implementation in the GNU LibC library[^]. The rand functions are located in the \glibc-XX\stdlib folder.
Best Wishes,
-David Delaune
|
|
|
|
|
OK thank you. I have Visual Studio 6, and I found the elusive 'rand.c' file I will take a look at it, thanks for the help!
KOM UIT DAAAAA!!!
|
|
|
|
|
If you use % (as in rand() % 50 ) on a random number, the distribution of its values will change - by a lot.
I know that's not what you asked, so this isn't an answer, and for many uses of rand it doesn't even matter.. but it's something to keep in mind.
Oh and % (1<<n) can be replaced by & ((1<<n)-1) which is a lot faster - you probably knew that (everyone does) but I saw a % 32768
|
|
|
|
|
I know that there will be many values of next for every possible random number. I calculated that for the random generator I have, that there will be almost 1311 values of 'next' for every random number.
I got this by: (32768/50)*(65536/32768)=65536/50 = 1311.
I was hoping to predict the next random number in a series by using these 1311 values to see which of those values would give the next random number.
I order to do this I need the definition of the random function used and I also need x amount of previous random numbers in order to predict the next. It seems from everybodie's replies that there are many different random number generators, I was hoping that there was a common function used by most programmers.
but thanks anyway...
KOM UIT DAAAAA!!!
|
|
|
|
|
Are you using this for encryption? If not, you might leverage mersenne twister's random
//generates a random number on [0,0xffffffff]-interval
The MT has a period of at least 2^19937 - 1 back a few years ago and is probably larger now.
Mersenne Twister is basically for Monte-Carlo simulations - it is not cryptographically secure "as is".
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html[^]
Are simply trying to set the seed on the rand() function or learn how to crack linear congruential generators?
modified on Saturday, June 13, 2009 5:03 AM
|
|
|
|
|
This helps a LOT! I was looking for a number generator that can be so 'random' as this mersenne twister. I am looking at methods for cracking random number generators, do you know of any good source of literature on this subject? I have googled it, but don't know which info is good, and which is junk.
I am also wondering how hard is it to crack a cryptographically secure number generator? Is it impossible or is it so difficult that it is impractical to even try. I mean, will you need thousands/millions or billions of iteration values before you can start to predict the next iteration?
thanks
KOM UIT DAAAAA!!!
|
|
|
|
|
|
I am developing a printing application under windowsXP, I have a problem with Custom the user papersize during printing. accordance to Microsoft, writing the change papersize function as follows.
LPDEVMODE CFileInfoView::ChangeDevMode(HWND hWnd, char *pDevice)
{
HANDLE hPrinter;
LPDEVMODE pDevMode;
DWORD dwNeeded, dwRet;
FORM_INFO_1 formInfo;
if (!OpenPrinter(pDevice, &hPrinter, NULL))
return NULL;
dwNeeded = DocumentProperties(hWnd,
hPrinter,
pDevice,
NULL,
NULL,
0);
pDevMode = (LPDEVMODE)malloc(dwNeeded);
//twice
dwRet = DocumentProperties(hWnd,
hPrinter,
pDevice,
pDevMode,
NULL,
DM_OUT_BUFFER);
if (dwRet != IDOK)
{
free(pDevMode);
ClosePrinter(hPrinter);
return NULL;
}
formInfo.Size.cx=176000;
formInfo.Size.cy=330000;
formInfo.Flags=FORM_USER;
formInfo.ImageableArea.left=0;
formInfo.ImageableArea.top=0;
formInfo.ImageableArea.right=176000;
formInfo.ImageableArea.bottom=330000;
formInfo.pName="Custom Paper(176*330)";
LPTSTR FormName=new char[32];
if(!GetForms(hPrinter,formInfo.Size,FormName))//the function Query the form is exiting
{
if(!AddForm(hPrinter,1,(LPBYTE)&formInfo))//add the custom papersize
{
DWORD dd=GetLastError();
free(pDevMode);
ClosePrinter(hPrinter);
return NULL;
}
//make sure the form is added
if(!GetForms(hPrinter,formInfo.Size,FormName))
{
AfxMessageBox("");
return NULL;
}
}
memcpy(pDevMode->dmFormName,FormName,strlen(FormName));
pDevMode->dmFormName[strlen(FormName)]='\0';//must null-terminated
pDevMode->dmFields=DM_FORMNAME;
dwRet = DocumentProperties(hWnd,
hPrinter,
pDevice,
pDevMode,
pDevMode,
DM_IN_BUFFER |
DM_OUT_BUFFER);
ClosePrinter(hPrinter);
if (dwRet != IDOK)
{
free(pDevMode);
return NULL;
}
delete [] FormName;
return pDevMode;
}
void CFileInfoView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo )
{
CScrollView::OnPrepareDC(pDC, pInfo);
if(pDC->IsPrinting())
{
if(IsWindowNT())//make sure Windows Version is support
{
Char* PrinterName = NULL;
CString s=pInfo->m_pPD->GetDeviceName();
PrinterName=s.GetBuffer(s.GetLength());
lpDevMode = ChangeDevMode(m_hWnd,PrinterName);
pDC->ResetDC(lpDevMode);
}
else
{
.....
}
}
}
However, after running, and some printers can be changed for custom paper, but some of the default paper, has not changed.why? please help me. thank you!
modified on Friday, June 12, 2009 12:12 PM
|
|
|
|
|
|
Hi I am devloping an application in MFC
I need to convert UCAHR pointer to Cstring and vice versa
such as
UCHAR *pMsg;
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[3] = '3';
pMsg[4] = NULL
to CString and viceversa...
Can anybody help.
|
|
|
|
|
uchar * to cstring?
CString ps;
UCHAR *pMsg=new UCHAR[4];
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[2] = '3';
pMsg[3] = '\0';
ps=CString(pMsg);
|
|
|
|
|
reddy07 wrote: UCHAR *pMsg;
pMsg[0] = '1';
pMsg[1] = '2';
pMsg[3] = '3';
pMsg[4] = NULL
As it stands, there is a access violation on your horizon.
Anyway the CString constructor is probably able to handle your unsigned char array, for instance try
CString s((char*) pMsg);
On the other hand, if you need to convert the CString value to an array of unsigned char s, then the effort required may depend on the type of build you are doing, UNICODE or not.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello!
If you wanted a deeper understanding of the topic, I'll recommend you this article: CString Management[^].
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Thank you guys, it was very helpful.
|
|
|
|
|
I'm using WMI to get the hardware configuration of a machine. It all works fine but there is something that is bothering me. If, for example I want to find out how much RAM was in a machine I would look in Win32_PhysicalMemory and get the Capacity property, this then gives me back a string. Is there any way to get an actual number out of it?
If I have:
CIMTYPE ableugh;
if(classObj)
{
BSTR strClassProp = StringToBSTR("Capacity");
classObj->Get(strClassProp, 0, &v, &ableugh, 0);
SysFreeString(strClassProp);
}
My expertly named variable ableugh says that the information is a CIM_UINT64 which isn't too far from the truth, but the VARIANT still just contains a BSTR . I can of course just convert the string, but if there's another way I think I would prefer it.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
What does v.ullVal contain?
ULONGLONG ui64Val = v.ullVal;
Best Wishes,
-David Delaune
|
|
|
|
|
I checked most of the numerical values in the VARIANT , a 1Gig stick of ram should be 1073741824, the values contained within ullVal , uiVal , iVal , lVal etc. all contained something like 48756. I don't remember the exact number.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
See if this works for you:
...
VARIANT v;
CIMTYPE ableugh;
hr = classObj->Get(L"Capacity", 0, &v, &ableugh, 0);
VariantChangeType(&v, &v, 0, VT_UI8);
ULONGLONG ui64Val = v.ullVal;
...
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks for the help, VariantChangeType seems to be what I need. All I have to check for now is if the variant is NULL. CIMTYPE might come out as a string, but the variant could still be NULL, hr is okay and VariantChangeType doesn't complain, but when you try to get the BSTR kaboom!
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
VariantChangeType[^] - just a string conversion under the hood, but you don't have to do the work yourself!
Use
::VariantChangeType(&ableugh, &ableugh, 0, VT_UI8);
and then V_UI8(&ableugh) should yield the result you want.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
That works out pretty good, thanks.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Hello,
previously i have posted in wrong forum.
Problem is - User can input any number, program need to take decision based on inputs.
Let suppose input is stored in variable n;
based on value of n some function will be called.
program logic can be -
if(n > 1 && n < 10 )
callfun1();
if(n > 11 && n < 20 )
callfun2();
if(n > 25 && n < 30 )
callfun3();
if(n > 33 && n < 38 )
callfun4();
if(n > 1 && n < 10 )
callfun5();
.
.
.
and so on
if(n > minlimit && n < maxlimit )
callfunX();
Is there any easy way to avoid if else chain to do similar work.
Or is there any way to change the limits in if condition at some central place something using #define.
Please provide your inputs/help to help me finding other ways to optimize programming.
Thank
Vikas
vicky
|
|
|
|
|
You could arrange them in such a way that they effectively do a binary search. That wouldn't bring the total number of tests down but it would run faster (singe every time execution flows through it it can only take 1 path, so O(log n) steps instead of O(n))
Or you could fill a giant array with function pointers for O(1) time.
Or maybe you could change the functions to not need to be selected anymore (using funny bit magic if necessary) but whether that's possible depends on the function of course.
|
|
|
|
|