Solved

# Random string generation using rand()

Posted on 2001-06-07
322 Views
I want to create a string of seven random characters containing a combination of small letters a-z and numbers 0-9. e.g. "w4dg7hg" or "3ghtry8".
Can anyone please give me the code for this using rand()?
0
Question by:ennkay

LVL 8

Expert Comment

ID: 6166344
std::string s;
for(int i = 0; i < 7; ++i)
{
int number = rand() % 36;
if(number < 26)
s += (number + 'a');
else
s += ((number - 26) + '0';
}
0

Author Comment

ID: 6166390
Looks good but i can't use std::string. Can you modify the code using a char array?
0

Expert Comment

ID: 6166541
// note a_nSize is buffer size of o_zStr include NULL character
void RandomString(char* o_zStr,const int a_nSize)
{
o_zStr[a_nSize-1]=NULL;
int nNumber;
for( int i = 0 ; i < a_nSize-1 )
{
nNumber=rand()%36;
if( nNumber < 26  )
o_zStr[i]=(nNumber+'a');
else
o_zStr[i]=(nNumber-26+'0');
}
}
0

LVL 6

Accepted Solution

zebada earned 150 total points
ID: 6166545
mnashadka should still get the points but here's the C version...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
char *c,s[8];
int  i,n;

srand(time(0));
for ( c=s,i=0 ; i<7 ; i++,c++ ) {
n = rand() % 36;
if ( n<26 )
*c = n+'a';
else
*c = (n-26) + '0';
}
*c = '\0';
printf("%s\n",s);
}
0

LVL 6

Expert Comment

ID: 6166547
happylin - you beat me by one minute - that's close
0

Author Comment

ID: 6166830
I have accepted your answer since it plugs in better in my code. A couple of question:
Why time(0) for random seed?
What does rand()%36 mean?

0

LVL 6

Expert Comment

ID: 6167055
time(0) will return a different value each time it is called - therefore the random number generator will return a different sequence of random numbers each time the program is run. Although looking at the documentation any value other than 1 will do the same thing. So I guess srand(2) would be just as effective as srand(time(NULL)). I just never read the doco carefully before.

rand() will return a number between 0 and RAND_MAX. The modulus operator % will give us the remained of the random value when divided by 36 which in effect converts the range of values to 0..35 which is the same as the number of alpahnumeric characters that you wanted to use for your string.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

#### 810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.