A 2-D Array of random numbers that don't repeats itself

This is a discussion on A 2-D Array of random numbers that don't repeats itself within the C Programming forums, part of the General Programming Boards category; I guess i am writing the correct code but still the output is not proper. Here's the algorithm i am ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    A 2-D Array of random numbers that don't repeats itself

    I guess i am writing the correct code but still the output is not proper. Here's the algorithm i am trying-

    ->generate a random number using rand() inside the array
    ->using other variables, check if there is the value generated is already assigned to some other slot
    -> if not, assign thee vaue to the slot

    here's quote of my code-

    Code:
    for ( i=0; i<4; i++)
        {
            for( j=0; j<4; j++)
            {
                 z=(rand()%15)+1;
                 for ( k=0; k<4; k++)
                     {
                       for( l=0; l<4; l++)
                       {
                          if (a[k][l]==z)
                          break;
                       }
                     }
            }
        }
    please suggest necessary changes.

    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    If the random numbers are within a small range, a simpler solution is to treat the 2D array as one big 1D array, then perform a Fisher-Yates/Knuth shuffle on the array that is pre-populated with the numbers from the range.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Akshay_ View Post
    please suggest necessary changes.
    I assume that you then want to know the minimum that you need to change to get it to do what you want. I.e. you have no interest in any other suggestions.

    The minimum would be to:
    Add another loop in the middle such that if the number randomly generated is already in use then you generate another.
    Generate 16 distinct values for your 4x4 array instead of only 15, which would make it try forever to find a valid last value.
    Actually put the randomly chosen value into the array.
    Change your break to something that breaks out of both the innermost loops rather than just the one. A flag variable might help there.

    Or you could just delete the code you have, do a Fisher-Yates shuffle and use two nested loops to just stick those values into the array.

    I also recommend the later option.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random numbers into array
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 05-31-2012, 04:14 PM
  2. Picking random numbers from an array
    By toyganozy in forum C Programming
    Replies: 2
    Last Post: 04-06-2012, 08:52 PM
  3. Random number generation without repeats
    By kishore84 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 12:17 PM
  4. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 06:53 PM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 03:45 AM

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