On Fri, 09 Jan 2004 09:52:19 +0800, Phipps Xue wrote:
Hello All,
I wanna write an small game which need to generate a random string each
time. It should go like this.
TCHAR tmpStr[_MAX_PATH];
unsigned int nLen=50;
TCHAR seedStr=_T("dummy seed");
int ret = randomstr(tmpStr, /*returned random string*/
nLen /*length of returned string*/,
seedStr/*a seed for string producing*/);
Is there any library available or any helpful information?
I'm not aware of any library. Also, you don't give any information about
what goes in the string. It probably is pretty easy to write yourself if
the requirements on the string are easy.
You don't need a seedstring, just seed the random generator.
Something like:
int randomstr(char *buf, size_t len)
{
int i=0;
char c;
if (len==0) return 1;
while (i<len) {
c = rand() % SCHAR_MAX; /* look at the FAQ for better techniques */
if (isprint(c)) {
*buf++ = c;
i++;
}
}
return 0;
}
HTH,
M4