Thread: non-repeating random numbers

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    non-repeating random numbers

    i'm supposed to generate 7 random numbers whereby each number can be any number between 1 to 45. but i need to ensure that there're no duplicates in those 7 numbers

    Code:
    #include <stdio.h>
     
    int main(void)
    {
         int count, i=1, r_num[7];
     
         srand (time(0)); /* seed is determined at run-time by the system */
     
         for(count=0; count<7; count++)
             r_num[count] = rand()%45 + 1;
     
     
         for(i=0; i<7; i++)
         {
             for(a=0, a<7; a++)
             {
                 if(i == a)
                     continue;
                 if(r_num[i] == r_num[a])
                     r_num[a] = rand()%45 + 1;
             }
         }
    these are my codes..but im stuck..can anyone help me?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    There are at least 2 ways to achieve the goal:

    1.
    Code:
    Step 1 Generate a number a1 from  1 to 45
    Step 2 Generate a number a2 from 1 to 44
    	If (a2 >= a1) a2++;
    Step 3 etc
    2.
    Code:
    Step 1 Generate a number a1 from 1 to 45
    Step 2 Generate a number a2 from 1 to 45
    	If (a2 == a1) goto Step2
    Step 3 etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There are two obvious solutions. Since your range is small, an efficient way would be to fill an array with sequential numbers and then randomly shuffle the array. The first 7 are your numbers:
    Code:
    int array[45];
    int i;
    
    for ( i = 0; i < 45; i++ )
      array[i] = i + 1;
    
    shuffle ( array, 45 );
    I'd recommend trying to write the shuffle first before asking for help, because it's a good learning experience. The second obvious solution is inefficient, but you probably won't notice it with such a small range. For each number you generate, you check to see if it's already in the array. If it is, start over:
    Code:
    int i = 0;
    
    while ( i < 7 ) {
      int r = rand() % 45 + 1;
      int j;
    
      /* Search the existing numbers */
      for ( j = 0; j < i; j++ ) {
        if ( array[j] == r )
          break;
      }
    
      /* If we broke early, don't do anything */
      if ( j == i )
        array[i++] = r;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    actually im still quite new to C. so im not really sure what does shuffle function do. mind to explain?
    and im supposed to print out the 7 numbers. so how do i get them out of the 'shuffled array'?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do you use winamp? what does the shuffle function do there?

    you reorder you array in the ramdom order - then take 7 first numbers from it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    ya i do know what shuffle means. but is the function shuffle in the standard library or sumthing?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by redx2evil
    ya i do know what shuffle means. but is the function shuffle in the standard library or sumthing?
    Quote Originally Posted by Prelude
    I'd recommend trying to write the shuffle first
    Nope. Don't think so. It's all upto you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    Smile

    ok thanks

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is in C++

    But a simple way to do it yourself goes like that:
    Code:
    step 1:
    pick random number from 0 - (size-1);
    swap array[0] and array[picked_number];
    
    step 2:
    pick random number from 1 - (size-1);
    swap array[1] and array[picked_number];
    
    ...
    
    step (size-1)
    pick a random number from (size-2) - (size-1);
    swap array[size-2] and array[picked_number];

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    Smile

    oh ya..thanks a lot prelude

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Me too, me too!
    Code:
    int array[45] = {0}, x = 0;
    while( x < 7 )
    {
        if( array[ rand() % 45 ]++ == 0 )
            x++;
    }
    
    /* print non-zero element's index */
    Same song, second verse:
    Code:
    int array[ 45 ] = {0}, x = 0, y = 0;
    
    for( x = 0; x < 45; x++ )
        array[ x ] = x + 1;
    
    for( x = 0; x < 7;  )
    {
        y = rand() % 45;
        if( array[ y ] )
        {
            array[ y ] = 0;
            x++;
        }
    }
    
    /* print zero element's index */
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. 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
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM