How to make an array with random numbers and some conditions?

I need to make an array (matrix) with random numbers but without any dupplicates an also with some conditions. I have this code, It can make random numbers without repetition but I have a variable called DifferentNumber and the random number must be different of that number

Matriz2[0][i] = (int) (Math.random() * range);
for (i = 1; i < cantidad; i++) {
    Matriz2[0][i] = (int) (Math.random() * range);
    for (int j = 0; j < i; j++) {
        if (Matriz2[0][i] == Matriz2[0][j]) {
            i--;
        }
    }
}

For example,

DifferentNumber = 25

And the matrix must go from 0 to 45 without repetition and being different of 25 something like this:

Matrix [0][0] = 26
Matrix [0][1] = 1
Matrix [0][2] = 40
Matrix [0][3] = 39

Upvotes: 0

Views: 141

Answers (4)

s4r4z1n
s4r4z1n

Reputation: 7

To complete answer of Arnaud :

List<Integer> randomNumbers = new Random().ints(45,0, 45) //45 items in range 0-45
            .distinct() //don't allow duplicate number
            .filter(i -> i != 25) //or any condition you want
            .boxed()
            .collect(Collectors.toList());


   Collections.shuffle(randomNumbers);

Upvotes: 0

Manuel Manhart
Manuel Manhart

Reputation: 5475

You can easily extend your code to that like this:

Matriz2[0][i] = (int) (Math.random() * range);
for (i = 1; i < cantidad; i++) {
    Matriz2[0][i] = (int) (Math.random() * range);
    // add this if / else statement
    if (Matriz2[0][i] == DifferentNumber) {
        i--;
    } else {
        for (int j = 0; j < i; j++) {
            if (Matriz2[0][i] == Matriz2[0][j]) {
                i--;
            }
        }
    }
}

an even nicer solution (much like Trevor suggested would we based on set - i put this into a function:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test {

    public static void main(String[] args) {
        Set<Integer> exclude = new HashSet<>();
        exclude.add(4);
        exclude.add(6);
        exclude.add(7);
        for (int[] arr : getRandomMatrix(10, 15, exclude)) {
            for (int i : arr) {
                System.out.println("rnd: " + i);
            }
        }
    }

    public static int[][] getRandomMatrix(int cantidad, int range, Set<Integer> excludeNumbers) {
            int[][] Matriz2 = new int[1][cantidad];

            Set<Integer> randomNumbers = new HashSet<>();
            while (randomNumbers.size() < cantidad) {
                int randomNumber = -1;
                while(randomNumber == -1 || excludeNumbers.contains(randomNumber)) {
                    randomNumber = (int) (Math.random() * range);
                }
                randomNumbers.add(randomNumber);
            }

            List<Integer> list = new ArrayList<>(randomNumbers);
            for (int i = 0; i < list.size(); i++) {
                Matriz2[0][i] = list.get(i);
            }
            return Matriz2; 
    }
}

Upvotes: 2

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31243

If the numbers stay low, you can put all of them in a list (except 25 for example) then shuffle it :

List<Integer> randomNumbers = new ArrayList<>();
for (int i = 0; i < 100; i++) {
    if (i != 25) {
        randomNumbers.add(i);
    }
}
Collections.shuffle(randomNumbers);

Or in a Java 8 way :

List<Integer> randomNumbers = IntStream.rangeClosed(0, 100)
        .filter(i -> i != 25) //or any condition you want
        .boxed()
        .collect(Collectors.toList());
Collections.shuffle(randomNumbers);

Now, you can just take them one by one. Here, the more convenient is the iterator :

Iterator<Integer> iterator = randomNumbers.iterator();

for (row = 0; row < 10; row++) {
  for (int col = 0; col < row; col++) {
    matrix[row][col] = iterator.next();
  }
}

Upvotes: 0

Trevor Freeman
Trevor Freeman

Reputation: 7242

One reasonably easy approach is to create a Set and pre-generate your random numbers into it, until the set contains as many numbers as you need. Then loop through your matrix and the Set, assigning a number from the Set for each position in the matrix.

When adding a number to the Set you can simply exclude your number of concern.

Upvotes: 2

Related Questions