-2

I'm using the following code for generate the random numbers in C programming, but for every compilation a particular set of random numbers are generating repeatedly. Can anybody mention the correction here???

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{   
  int i,a[40];

  for(i=0;i<40;i++)
  {    
    a[i] = rand() % 6;
    printf("%d\n", a[i]);
  } 
  getch();
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    You've used the wrong tag. This isn't a C# question. – Martin Costello Apr 28 '14 at 17:42
  • You aren't initializing the random seed correctly. There are multiple correct answers below which address this. – Cloud Apr 28 '14 at 18:00
  • 1
    This question is asked repeatedly; please show some effort and do a search before asking the question. You will get your answer faster that way and not waste anyone's time. It's a win for everyone. – Eric Lippert Apr 28 '14 at 18:06

3 Answers3

4

You need to initialize random seed to a different value, please see http://www.cplusplus.com/reference/cstdlib/rand/

Bogdan
  • 484
  • 2
  • 7
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Andrew Barber Apr 28 '14 at 18:31
  • @AndrewBarber Yeah, but I am kinda lazy, please downvote so that the other answer gets on top :) – Bogdan Apr 28 '14 at 18:35
2

Per the documentation,

If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1. [emph. mine]

You need to mix it up, something like this:

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

main()
{   
  int i,a[40];

  // seed the PRNG with some random value
  // the current time is often used. You might want to incorporate
  // other sources of entropy as well.
  srand(time(NULL)); 

  for( i = 0 ; i < 40 ; i++ )
  {    
    a[i] = rand() % 6;
    printf("%d\n", a[i]);
  } 
  getch();
}

One should note that

  • seed the PRNG just once at beginning of execution
  • The reason for incorporating more sources of entity, rather than just relying on the current system clock is that the system clock itself is not very random -- to invocations quite close to each other my result in similar sequences. Other good sources of entropy include:

    • hard disk statistics
    • network I/O
    • process activity
    • keystroke latency
    • mouse movements
    • etc.

Although looking at user interface components such as keystrokes and mouse movements won't buy you much in the way of entropy if your software is running, say, on a server, where the keyboard and mouse are likely to be idle for long periods of time.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

Do something like this (inserted line) to seed the random number generator:

  //...
  int i,a[40];
  srand(clock());  //inserted line
  for(i=0;i<40;i++)
  //...

The clock() function will provide a different seed value each clock tick, resulting in a new and different initialization to the rand() function.

ryyker
  • 22,849
  • 3
  • 43
  • 87