user3634316
user3634316

Reputation: 5

Store random numbers in an array without adjacent repeats

I want to store random numbers in an array without repeats with in the range 0-9. It is fine if they repeat but not if they are right next to each other like: 1577984 is incorrect(because of the double 7s) but 151515 is theoretically okay.

I've been working at this a day or two now and this is my original base code:

public static int[] createRandomNumber(int characters){
    int randomNumberArray[] = new int[characters];
    for(int i =0; i<characters; i++){
        randomNumberArray[i] = (int)(Math.random()*9);
    }
    return randomNumberArray;
 }

And this is what I have now but it is doing the exact opposite of what I want it's generating all of the same number:

public static int[] createRandomNumber(int characters)
{
    int randomNumberArray[] = new int[characters];
    int random = (int)(Math.random()*9);
    for(int i = 0; i <characters ; i++)
    {
        randomNumberArray[i] = random;
        if(randomNumberArray[i] == random)
            randomNumberArray[i] = (int)(Math.random()*9);
        else if(randomNumberArray[i] != random)
            randomNumberArray[i] = random;
    }
    return randomNumberArray;
}

Is there a simple solution that I am just not seeing? I've probably rewritten this method 50 times.
Just so you know the parameter code is

int characters = in.nextInt();

so just the length of their randomly generated password that they want. Also I know that Math.random is pseudo-random but this isn't for real or anything. Plus there's more methods that generate random letters and such as well. (And yes I've done lots of research but no one seems to have my particular problem and believe me asking questions is a last resort for me.)

Thanks for any suggestions.

Upvotes: 0

Views: 617

Answers (2)

pjs
pjs

Reputation: 19855

Here's another possible solution based on a do-while:

class TestArrayGen {
   public static int[] createRandomNumber(int characters) {
      int randomNumberArray[] = new int[characters];
      randomNumberArray[0] = (int)(Math.random() * 10);
      for(int i = 1; i < characters; i++) {
         do {
            randomNumberArray[i] = (int)(Math.random() * 10);
         } while(randomNumberArray[i] == randomNumberArray[i-1]);
      }
      return randomNumberArray;
   }

   public static void main(String[] args) {
      for(int number : createRandomNumber(20)) {
         System.out.print(number + " ");
      }
      System.out.println();
   }
}

Note that I've also boosted the multiplier to 10, you'll never get a 9 otherwise.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

In the for loop, you are assigning random to the number array. But then you are immediately checking if random is equal to that spot you just assigned, which is always true. So it picks another random number, which also may or may not be equal to the previous digit.

I've rewritten the for loop to have a while loop in its body, to keep checking versus the previous digit to see if there is a repeated digit.

for(int i = 0; i <characters ; i++)
{
    randomNumberArray[i] = random;
    // If not first, check if it matches the previous digit
    while(i >= 1 && randomNumberArray[i - 1] == random)
    {
        random = (int)(Math.random()*9);
        randomNumberArray[i] = random;
    }
}

Upvotes: 1

Related Questions