• Status: Solved
• Priority: Medium
• Security: Public
• Views: 202

A random string

I need to make a random string. I have to make sure that each time it a different string. I thought to combine the date and the time to a string. How can I do it?
0
osnat
• 4
1 Solution

Commented:
int nStrLen = 10;
string s;
srand( (unsigned)time( NULL ) );
for( int i = 0;   i < nStrLen;i++ )
s += rand();

nil_dib
0

Author Commented:
That will not promise me a DIFFERENT string EVERY time!
I thought to combine the date and the time, and since the time is changing I'll get every second a different string:
for example:
The date is 070999
The time is 10:00:00
So the final string is: 07099910:00:00
After 1 second time will be 10:00:01 so I'll get a new string: 07099910:00:01
This is what I want to do!
0

Commented:
they are different: the date and time are calculated in the time() function!

srand( (unsigned)time( NULL ) );

if you want the date and time as string:

CTime       time;
time = CTime::GetCurrentTime( );
CString s = t.Format( "%A, %B %d, %Y" );
// output in s:
// "Friday, March 19, 1999"

nil_dib
0

Commented:
for a string like 07099910:00:00
write

CString s = t.Format("%d%m%Y%H:%M:%S");

nil_dib
0

Commented:
why did you give a "C" ?
isn't the answer what you are looking for ???

nil_dib
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.