| | |
problem in generating non repeated random numbers
![]() |
•
•
Join Date: Jul 2005
Posts: 4
Reputation:
Solved Threads: 0
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
#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
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.
(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.
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:
Not only is it easier to verify, it's also considerably faster.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 157 int main ( void ) { int seq[N]; int i; srand ( (unsigned)time ( NULL ) ); /* Initialize seq to an ordered range */ for ( i = 0; i < N; i++ ) seq[i] = i; /* Random shuffle */ for ( i = 0; i < N - 1; i++ ) { int r = ( rand() % ( N - i ) + 1 ); int save = seq[i]; seq[i] = seq[i + r]; seq[i + r] = save; } /* Test the sequence */ for ( i = 0; i < N; i++ ) printf ( "%d ", seq[i] ); return 0; }
I'm here to prove you wrong.
Sometimes the standard library makes life easier:
Obviously not useful since we already have an implementation, but useful if you'll need it elsewhere.
C Syntax (Toggle Plain Text)
#include <algorithm>
C Syntax (Toggle Plain Text)
/* Random shuffle */ std::random_shuffle(seq, seq + N);
Obviously not useful since we already have an implementation, but useful if you'll need it elsewhere.
>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.
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.
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
- not getting non repeating random numbers many times (C)
Other Threads in the C Forum
- Previous Thread: help needed in assigning values..
- Next Thread: Need help with programming!
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






