random string generator library!

P

Phipps Xue

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?

TIA

Phipps
 
M

Martijn Lievaart

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
 
S

Sasha Goldshtein

You could use simple permutations of the seed string, if that's
sufficient for your needs. There are permutation algorithms in the STL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top