954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

not getting non repeating random numbers many times

hi
i want to generate non repeated random numbers many times. for that i m using the following code.

#include "sys/types.h"
#include "stdio.h"
#define MAX 200
#define N 20
main()
{
	int array[N],r;
	int n = 0; int count_check,count_gen ,i,j;

	for(j=0;j<10;j++,printf("\n"))
	{
		srand(j);
		  for (count_gen=0;count_gen<=MAX;count_gen++)
		  {
		    r = rand()%N;
		    for ( count_check = 0; count_check < n; count_check++ )
		    {
		      if ( r == array[count_check] )break;
		    }// end for count_check.
		  if ( count_check == n ) array[n++] = r;
		  }// end for count_gen.
		for(i=0;i<N;i++)
		printf("%d\n",array[i]);
	}// end for j.

}//  end of main.


<< moderator edit: added [code][/code] tags >>

it generates random numbers from 0 to 20 without repeating in an array. but it generates only once. next time in the for loop ( in j ) it gives the same pattern again , which i dont want. can some body please help me to get different pattern every time in the for loop ( for j ).

xshashiy
Newbie Poster
4 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Call srand once in a program, before the loop.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Call srand once in a program, before the loop.


I have tried that but it does not work. it produces same pattern of numbers.


regards
shashi

xshashiy
Newbie Poster
4 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 
I have tried that but it does not work. it produces same pattern of numbers.

Using the same seed will produce the same pattern. Oftensrand(time(NULL)); is used, but read the link prog-bman posted.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Hi frnd i am sivaram,
first you include these header files include,include
and then in main() you put "srand(time(NULL));" after your initializations and before rand(); function every time you will get different output

psrknellai
Newbie Poster
1 post since Feb 2011
Reputation Points: 7
Solved Threads: 0
 
Hi frnd i am sivaram, first you include these header files include,include and then in main() you put "srand(time(NULL));" after your initializations and before rand(); function every time you will get different output


I generally don't like to do this, exactly because you get a "completely" random sequence each time.

Random number generators generally start at a number and then move through a sequence of other (seemingly) random numbers. However, the list of numbers that you get is determined by the initial number. There is a different sequence generated for each starting point.

When working with random numbers, most of the time you are going to want to try andreproduce the results a number of times, for debugging and such if nothing else. If you use the method suggested above, you won't be able to do this, since you won't ever know what the starting point was, and so will never be able to retrace the path that the random number generator takes. My advice would be to either always use the same seed (so you get the same (random-looking) path from the generator) or, if you must use a different sequence each time, at least write the results to a file and get the program to output the seed that was used and store it in the file. In either case, srand(time(NULL)) is never going to be good, since you can't possibly recover the seed. If you have to generate a seed this way, do:

unsigned seed = time(NULL);
srand(seed);

That way, you can write the seed to file with the results of your program later and you have a chance at reproducing the results if you need to.

EDIT: Sorry, just noticed that this thread is from 2005. My bad. It should probably be closed or something!

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

Yes all you need to do is use
srand(time(NULL));

but make sure to add
# include and # include

slygoth
Light Poster
43 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You