vendredi 26 avril 2019

Random generator creates the same numbers

While looping through a for loop which is randomly 1-10 long. I also go through a while loop. I am trying to find 6 random numbers between 1-42. I then do that again (1-10) times. Currently, these 6 random numbers stay the same. I suspect it has something to do with the seed but I could not solve it.

I tried to use both SecureRandom, Random, and via Math.random() Nothing has worked.

    private Random random = new Random();

    private int getRandomInt(int min, int max) {
         return random.nextInt((max - min) + 1) + min;
    }

    public ArrayList<LottoTip> createOpponentTips() {
        ArrayList<Integer> opponentChosenNumbers = new ArrayList<>();
        ArrayList<LottoTip> lottoTips = new ArrayList<>();

        //1-10
        int amountOfTips = getRandomInt(MIN_TIPS, MAX_TIPS);

        for (int i = 0; i < amountOfTips; i++) {

            int[] arrayOpponentChosenNumbers = new int[REQUIRED_NUMBERS];

            while (opponentChosenNumbers.size() < REQUIRED_NUMBERS) {

                //1-42 This here is the problem zone

                int randomNumber = getRandomInt(RANGE_NUMBER_MIN, RANGE_NUMBER_MAX);
                if (!opponentChosenNumbers.contains(randomNumber)) {
                    opponentChosenNumbers.add(randomNumber);
                }
            }
            //1-6 ---This here works
            int opponentLuckyNumber = getRandomInt(RANGE_LUCKY_NUMBER_MIN, RANGE_LUCKY_NUMBER_MAX);

            for (int j = 0; j < opponentChosenNumbers.size(); j++) {
                arrayOpponentChosenNumbers[j] = opponentChosenNumbers.get(j);
            }

            lottoTips.add(new LottoTip(arrayOpponentChosenNumbers, opponentLuckyNumber));
        }

        return lottoTips;
    }


Here is a System.out.println as you can see the Numbers are staying the same. Could you help me find a way to create a getRandomInt method which works all the time?

Thank you very much for your help.

Kerry York [
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 2, 
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 4, 
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 5, 
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 1, 
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 3, 
Numbers: 6, 4, 30, 15, 25, 5 Lucky Number: 6]

Don Dickinson [
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 6, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 5, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 3, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 2, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 4, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 1, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 1, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 4, 
Numbers: 23, 29, 34, 18, 16, 19 Lucky Number: 3]

Clifford Weinstein [
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 6, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 2, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 1, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 4, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 5, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 6, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3, 
Numbers: 6, 33, 13, 2, 37, 38 Lucky Number: 3]

Angela Spencer [
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 6, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 3, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 4, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 5, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 4, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 2, 
Numbers: 12, 39, 8, 25, 40, 15 Lucky Number: 3]

Leroy McIntosh [
Numbers: 5, 23, 2, 24, 28, 3 Lucky Number: 1]




Aucun commentaire:

Enregistrer un commentaire