non-repeating random numbers

This is a discussion on non-repeating random numbers within the C Programming forums, part of the General Programming Boards category; i'm supposed to generate 7 random numbers whereby each number can be any number between 1 to 45. but i ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21