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 
7
  • Please see this discussion on why not to cast the return value of malloc() and family in C.. Commented Mar 9, 2016 at 19:46
  • Move char* randomstring(int); at the beginning.... Commented Mar 9, 2016 at 19:46
  • 3
    From now on, please properly indent your code or else no one will be able to read it. Commented Mar 9, 2016 at 19:47
  • You should put srand((unsigned) time (NULL)) inside your function Commented Mar 9, 2016 at 19:47
  • 1
    @stanpines s/able/willing/ Commented Mar 9, 2016 at 19:47

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

if i remove srand function,so will it give correct output?
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)
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 ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.