By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

random string generator library!

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 22 '05 #2

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

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?

TIA

Phipps


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.