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 ).

Recommended Answers

All 7 Replies

Call srand once in a program, before the loop.

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

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

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

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

commented: Gee, I wish someone mentioned that back in 2005... -3

Hi frnd i am sivaram,
first you include these header files include<time.h>,include<stdlib.h>
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 and reproduce 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!

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

but make sure to add
# include <time.h> and # include <stdlib.h>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.