944,222 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6372
  • C RSS
Jul 4th, 2005
0

problem in generating non repeated random numbers

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xshashiy is offline Offline
4 posts
since Jul 2005
Jul 4th, 2005
0

Re: problem in generating non repeated random numbers

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.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 4th, 2005
0

Re: problem in generating non repeated random numbers

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 4th, 2005
0

Re: problem in generating non repeated random numbers

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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Jul 5th, 2005
0

Re: problem in generating non repeated random numbers

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 5th, 2005
0

Re: problem in generating non repeated random numbers

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! ;-) )
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Sep 2nd, 2010
0
Re: problem in generating non repeated random numbers
Oh man you rock. I'm a noob, and getting lost in following how the random shuffle for loop works...was wondering if you could explain line by line?

Click to Expand / Collapse  Quote originally posted by Narue ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DKdontKNO is offline Offline
1 posts
since Sep 2010

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: P2P programming in C
Next Thread in C Forum Timeline: Random numbers with exception of 4 digits





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


Follow us on Twitter


© 2011 DaniWeb® LLC