Solved

Random string generation using rand()

Posted on 2001-06-07
7
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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