Generate an array of random numbers that do not repeat in java

-1
public static void main(String[] args) {

    int numeros[]=new int [4];
    int minimo = 1;
    int maximo = 5;
//Genera, comprueba e introduce los numeros en el array si la comprobación es correcta
    for (int i = 0; i < numeros.length; i++) {
        int num = Generar(minimo,maximo);   
        //Aqui es donde falla, siempre sale true.
        if(Comprobar(numeros,num)==true){
            numeros[i] = num;
        }
    }

    for (int i = 0; i < numeros.length; i++) {
        System.out.println(numeros[i]);
    }

}
public static int Generar(int minimo, int maximo){
    Random  rnd = new Random();
    int num = (minimo + rnd.nextInt((maximo + 1) - minimo));
    return num;
}
public static boolean Comprobar(int numeros[], int num){
    boolean igual = false;
    for(int i = 0; i<numeros.length;i++){
        if(numeros[i] != num){ 
            igual = true;
        }
    }return igual; 
}
    
asked by Milayton Pereira 02.09.2018 в 13:43
source

2 answers

0

Well, you must make some modifications to the code

When you use the Check method, the cycle you set with For is done 4 times since "numeros.length" will always be equal to 4, and this is not necessary (or in fact gives unexpected behavior in the program ), in fact it would only be necessary to check, the values that until that moment are assigned in the array, now to know this, we would pass a third argument to this method, the variable "i". With this we will have to modify the method, in specific the Boolean operators must be inverted (since if we do not do it the first value assigned to "num" will return false.) The method would remain as follows:

public static boolean Comprobar(int numeros[], int num, int i){
 boolean igual = true;
 for(int j = 0; j<i; j++){
    if(numeros[j] == num){
            igual = false;
       }
   }return igual; 
}

Now when you send to call the method Check, only consider, the situation in which a true was returned, but in case of a false we should reassign a quantity to "num", it can be like this to redo the same step of the cycle:

if(Comprobar(numeros,num,i)==true){
                numeros[i] = num;
} else i--;

Notice the third argument in the method. As a last comment, I consider that the formula you assign to "num" would be enough with this:

int num = minimo + rnd.nextInt(maximo);

Luck.

    
answered by 03.09.2018 / 04:41
source
0

Look, the question is simple, you should not put a cycle into another cycle, just enough to check only once:

public static boolean Comprobar(int numeros[], int num){
//declaras una variable boolena y la inicia en falso
    boolean igual = false;
    for(int i = 0; i<numeros.length;i++){
//recorres el array normal preguntado a cada vuelta si son iguales
        if(numeros[i] == num){ 
//si en algun momento llegan a ser iguales entonces la variable cambia de valor
            igual = true;
        } 
    }
//y ya solo retornas la variable con su valor
    return igual; 
}

UPDATE will change if(numeros[i] == num) I hope you serve, you tell me bro ....

    
answered by 02.09.2018 в 16:37