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

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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, 03:14 PM
  2. Picking random numbers from an array
    By toyganozy in forum C Programming
    Replies: 2
    Last Post: 04-06-2012, 07: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, 02:45 AM