not getting non repeating random numbers many times

Reply

Join Date: Jul 2005
Posts: 4
Reputation: xshashiy is an unknown quantity at this point 
Solved Threads: 0
xshashiy xshashiy is offline Offline
Newbie Poster

not getting non repeating random numbers many times

 
0
  #1
Jul 6th, 2005
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 ).
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: not getting non repeating random numbers many times

 
0
  #2
Jul 6th, 2005
Call srand once in a program, before the loop.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 4
Reputation: xshashiy is an unknown quantity at this point 
Solved Threads: 0
xshashiy xshashiy is offline Offline
Newbie Poster

Re: not getting non repeating random numbers many times

 
0
  #3
Jul 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: not getting non repeating random numbers many times

 
0
  #4
Jul 7th, 2005
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: not getting non repeating random numbers many times

 
0
  #5
Jul 7th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC