1

I have written a program to generate a random string, but when i am calling the function for two/more times,i am getting same random strings.

Please check the code below:

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

char* randomstring(int length);

int main()
{
   char* randomstring(int);
   char *str, *str2;
   str = randomstring(3);
   str2 = randomstring(3);
   printf("final random string is %s and length is %s\n", str, str2);
}

char* randomstring(int length)
{
   int len, len1, i = 0, j = 0;
   char *c;
   char *string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   len = strlen(string);
   len1 = length + 1;
   time_t t;
   c=(char*) calloc(len1, sizeof(char));
   printf("final random string is %d \n", len);
   srand((unsigned) time(&t));
   for(i = 0; i < length; i++)
   {
      j=rand() % len;
      c[i] = string[j];
   }
   c[len1] = '\0';
   return c;
}

Output :

final random string is 26 
final random string is 26 
final random string is BNQ and length is BNQ
machine_1
  • 4,266
  • 2
  • 21
  • 42

2 Answers2

4

The function srand sets the random seed, and if you call it again with the same random seed, it will cause future random calls to return a repeat of the same sequence. So when you call:

srand((unsigned) time(&t));

it resets the random seed to the wall clock time in seconds. Which means if you call it again after less than a second has elapsed (as you do in your code), it probably resets the seed to the same value, causing you to get the same random string again.

You should only call srand once, at the start of your program in main, before calling any other function that calls rand

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • if i remove srand function,so will it give correct output? – Ashutosh Dalai Mar 09 '16 at 20:33
  • If you remove `srand` completely, the random number generator will be seeded with 0. So it will give "correct" output, but will give the same output every time you run the program. Putting the `srand` call in `main` will result in different output each time you run the program (assuming the clock ticks at least one second between runs) – Chris Dodd Mar 09 '16 at 21:34
1

First of all, for an allocation like

c=(char*) calloc(len1,sizeof(char));

later, using

c[len1]='\0';

is accessing out of bound memory. It invokes undefined behavior.

To elaborate, C uses 0-based indexing, so, by saying

 c[len1]='\0';

you're going off-by-one.

Then, you should call the PNRG seeder function srand() only once, possibly at main(). Otherwise, time() having a granularity of 1 second, it will seed the PNRG with same value which in turn will cause the rand() to produce the same set of random numbers again.

That said,

  1. Please see this discussion on why not to cast the return value of malloc() and family in C..

  2. sizeof(char) is guaranteed to be 1 in C.

A better and robust way of writing the allocation statement will be

c =  calloc( len1, sizeof*c );     
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261