problem in generating non repeated random numbers

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

problem in generating non repeated random numbers

 
0
  #1
Jul 4th, 2005
i want to generate non repeated random numbers from 0 to 156. here is the code for the purpose. but when i try to generate the random numbers more then 1 time by using a for loop, the same pattern on numbers repeats. how to solve this problem ?

#include "sys/types.h"
#include "stdio.h"
#include "time.h"
#define MAX 2000
#define N 156
main( )
{
int array[BIG_SIZE],r;
int n = 0; int count_check,count_gen ,i;
time_t t1;
(void) time(&t1);
srand48((long) t1);
for (count_gen=0;count_gen<=MAX;count_gen++)
{
r = lrand48()%N;
for ( count_check = 0; count_check < n; count_check++ )
{
if ( r == array[count_check] )break;
}
if ( count_check == n ) array[n++] = r;
}
for(i=0;i<N;i++)
printf("%d\n",array[i]);
}


regards
xshashiy
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: problem in generating non repeated random numbers

 
0
  #2
Jul 4th, 2005
You are using time for purposes of generating the random number, but only once before the loop. You should probably reset the time each part of the loop.

(void) time(&t1);

(Though you may continue to have the same problem as I think time only holds seconds... )

Also, what you posted won't compile - BIG_SIZE is not defined, etc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem in generating non repeated random numbers

 
0
  #3
Jul 4th, 2005
Since you already know the range of the sequence, and it's small, and you want non-repeating values, a random shuffle would be a better option than trying to check whether each new number was already generated or not:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define N 157
  6.  
  7. int main ( void )
  8. {
  9. int seq[N];
  10. int i;
  11.  
  12. srand ( (unsigned)time ( NULL ) );
  13.  
  14. /* Initialize seq to an ordered range */
  15. for ( i = 0; i < N; i++ )
  16. seq[i] = i;
  17.  
  18. /* Random shuffle */
  19. for ( i = 0; i < N - 1; i++ ) {
  20. int r = ( rand() % ( N - i ) + 1 );
  21. int save = seq[i];
  22. seq[i] = seq[i + r];
  23. seq[i + r] = save;
  24. }
  25.  
  26. /* Test the sequence */
  27. for ( i = 0; i < N; i++ )
  28. printf ( "%d ", seq[i] );
  29.  
  30. return 0;
  31. }
Not only is it easier to verify, it's also considerably faster.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,021
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: problem in generating non repeated random numbers

 
0
  #4
Jul 4th, 2005
Sometimes the standard library makes life easier:

  1. #include <algorithm>
  1. /* Random shuffle */
  2. std::random_shuffle(seq, seq + N);

Obviously not useful since we already have an implementation, but useful if you'll need it elsewhere.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem in generating non repeated random numbers

 
0
  #5
Jul 5th, 2005
>Sometimes the standard library makes life easier
Indeed. But since the original problem was clearly written in C, the C++ standard library is hardly useful. You might argue that C compiles as C++, so it's not a problem, but since you would be wrong, I recommend that you don't bother, and save yourself the intellectual manbeating I'll give you if you try.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,021
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: problem in generating non repeated random numbers

 
0
  #6
Jul 5th, 2005
Oh man, what was I thinking?

Never mind then. :o

(It's not a question of whether C can compile to C++; C++ simply can't be compiled as C! ;-) )
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