Thread: help with generating numbers

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    help with generating numbers

    Hi guys,
    Im trying to implement a function/program that generates numbers from 1 to the input number given by the user. I've come close to generating all the numbers however there are some duplicates of the numbers still remain.
    I just want to generate the number with no duplicates but my algorithm must be wrong, can anyone see anything wrong with what ive done?

    Cheers.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #define MAXSIZE 1000
    int main()
    {
    	int k;
    	int i;
    	int j;
    	int num;
    	int n=0;
    	int tmp[MAXSIZE];
    	printf( "Enter integer (max 1000): " );
    	scanf( "%d", &k );
    	srand(1234567);
    	for( i=1; ; i++ ) {
    		num = 1+rand()%k;
    			for( j=0; j<=MAXSIZE; j++ ) {
    				if ( num == tmp[j] )
    					break;
    				else if ( (num != tmp[j]) ) {
    					tmp[n++] = num;
    					break;
    				}
    			}
    			
    		if ( n==k )
    			break;
    	}
    	
    	for( i=0; i<=n-1; i++ )
    		printf( "%d ", tmp[i] );
    	
    	printf( "\n" );
    	return 0;
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    for( j=0; j<=MAXSIZE; j++ ) {
    if ( num == tmp[j] )
    break;
    else if ( (num != tmp[j]) ) {
    tmp[n++] = num;
    break;
    }
    }
    Look at that code again... it exits as soon as a match is found, and it exits as soon as a match is not found... hmm...
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I just want to generate the number with no duplicates

    Here's a way I'd done something similar, if you can stomach a goto.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hmm ive tried placing the tmp array outside of that for loop so that i allow it to scan the whole array for a match.

    however if it breaks out of that loop then it will add the value to the tmp array thereafter.
    there are only 10 people in the world, those who know binary and those who dont

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    the goto is hard to digest.
    but the if statements and what to do next are just as annoying.
    im stuck with the conditions.
    there are only 10 people in the world, those who know binary and those who dont

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >the goto is hard to digest.
    Then function-ize the inner loop to return true/false.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int foo(const int *a, size_t i)
    {
       size_t j;
       for ( j = 0; j < i; ++j )
       {
          if ( a[i] == a[j] )
          {
             return 1;
          }
       }
       return 0;
    }
    
    int main(void)
    {
       size_t i;
       int n[25];
       srand(time(NULL));
       for ( i = 0; i < sizeof(n)/sizeof(*n); ++i )
       {
          do
          {
             n[i] = rand() % sizeof(n)/sizeof(*n) + 1;
          } while ( foo(n, i) );
          printf("%2d ", n[i]);
       }
       putchar('\n');
       return 0;
    }
    [edit]
    Problems with this approach are mentioned here (if any kind of harmless registration is required, but you still fear entering, this is a teaser...)
    Imagine trying to fill each cell of a dart board with its own dart. If you're a bad player like me, everything will cruise along just fine *until* the last few squares, at which point I'll need tens if not hundreds (if not thousands!) of goes to try and fill the remaining squares.

    The above method suffers the same problem. You'll find that on slower machines of old, and when used against large arrays, this method can take a long time to complete! With a particularly bad rand() function, it may even lock up in an infinite loop.
    I recommend joining if only to read Peter's (or Paul's) posts.
    [/edit]
    Last edited by Dave_Sinkula; 08-01-2003 at 10:56 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    cheers dave
    there are only 10 people in the world, those who know binary and those who dont

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    if you perform the shuffle below, when u exchange the random numbers how will it know that it is not already in the array or not already generated?
    i.e. a duplicate occuring

    how can the shuffle not include a duplicate?

    ive seen in the card programs that deal say 52 cards with this shuffle algorithm, but im not sure how it deals/shuffles without no duplicates..
    there are only 10 people in the world, those who know binary and those who dont

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    sorry

    when you say unique, the same generated random number cannot appear again?
    there are only 10 people in the world, those who know binary and those who dont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  4. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM