Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Random string generation using rand()

Posted on 2001-06-07
7
Medium Priority
?
329 Views
Last Modified: 2010-04-02
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
Comment
Question by:ennkay
7 Comments
 
LVL 8

Expert Comment

by:mnashadka
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

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

Expert Comment

by:happylin
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Accepted Solution

by:
zebada earned 600 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

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

Author Comment

by:ennkay
ID: 6166830
zebada
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

by:zebada
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

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

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

Join & Ask a Question