944,025 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5522
  • C RSS
Jul 6th, 2005
0

not getting non repeating random numbers many times

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

  1. #include "sys/types.h"
  2. #include "stdio.h"
  3. #define MAX 200
  4. #define N 20
  5. main()
  6. {
  7. int array[N],r;
  8. int n = 0; int count_check,count_gen ,i,j;
  9.  
  10. for(j=0;j<10;j++,printf("\n"))
  11. {
  12. srand(j);
  13. for (count_gen=0;count_gen<=MAX;count_gen++)
  14. {
  15. r = rand()%N;
  16. for ( count_check = 0; count_check < n; count_check++ )
  17. {
  18. if ( r == array[count_check] )break;
  19. }// end for count_check.
  20. if ( count_check == n ) array[n++] = r;
  21. }// end for count_gen.
  22. for(i=0;i<N;i++)
  23. printf("%d\n",array[i]);
  24. }// end for j.
  25.  
  26. }// 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 ).
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xshashiy is offline Offline
4 posts
since Jul 2005
Jul 6th, 2005
0

Re: not getting non repeating random numbers many times

Call srand once in a program, before the loop.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 7th, 2005
0

Re: not getting non repeating random numbers many times

Quote originally posted by Dave Sinkula ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xshashiy is offline Offline
4 posts
since Jul 2005
Jul 7th, 2005
0

Re: not getting non repeating random numbers many times

Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Jul 7th, 2005
0

Re: not getting non repeating random numbers many times

Quote originally posted by xshashiy ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 10th, 2011
-2
Re: not getting non repeating random numbers many times
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
Reputation Points: 7
Solved Threads: 0
Newbie Poster
psrknellai is offline Offline
1 posts
since Feb 2011
Feb 10th, 2011
0
Re: not getting non repeating random numbers many times
Click to Expand / Collapse  Quote originally posted by psrknellai ...
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:
  1. unsigned seed = time(NULL);
  2. 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!
Last edited by ravenous; Feb 10th, 2011 at 10:00 am.
Reputation Points: 244
Solved Threads: 77
Posting Pro in Training
ravenous is offline Offline
450 posts
since Jul 2005
Feb 10th, 2011
0
Re: not getting non repeating random numbers many times
Yes all you need to do is use
srand(time(NULL));

but make sure to add
# include <time.h> and # include <stdlib.h>
Reputation Points: 10
Solved Threads: 0
Light Poster
slygoth is offline Offline
41 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Search string in a text file
Next Thread in C Forum Timeline: Im trying to pass arrays to and from a function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC