mercredi 27 janvier 2016

Make random number generator to not repeat same number twice [duplicate]

This question already has an answer here:

This is my first question, since I have started programming not very long ago. I have been trying to get a random number generator using two loops to not generate the same number twice. Last night I did spend 5 - 6 hours researching for the question did find some solutions but none worked for me. I have posted a code with my progression but it keeps generating the same number more than once.

I am sorry if the question I have posted has been answered before, if so please send me the link. This is a homework so the number generator method should not be changed as I have commented it.

Thank you in advance.

        int [] a1 = new int [5]; 
        for ( int i = 0; i < a1.length; i++) 
        {
            int numriSekret = (int) (Math.random() * 100)+1; // This should not be changed, every other part can change. 
                a1[i] = numriSekret;
                for(int j = 0; j < i; j++){
                if (a1[i] == a1[j]){
                    i--;
                    break;
                }   
            }
            System.out.println(vargu1[i]);  
            }




Aucun commentaire:

Enregistrer un commentaire