mardi 30 juin 2020

stackoverflowerror with recursive method that keeps on generating a series of random numbers that must meet certain set of conditions

I'm trying to write a program that implements a random number generator based on a lottery game. The game consist of 7 unrepeating numbers that has to be drawn among a range of 1-34 numbers. The interesting part is that it must meet a certain set of conditions.

The two conditions I have given is namely the odd/even numbers ratio and the distance between ball nr.1 and nr. 7. We asume that the numbers are sorted.

My problem is that I'm getting a stackoverflowerror after a few executions when I'm setting the odd/even ratio relatively low and the distance between ball nr.1 and nr.7 in a similiar way.

Here is my part of the code that regulates the two given conditions respectively:

Java

    //this method will set the maximum allowed distance 
    //between ball nr. 1 and nr.7
    public boolean diff1to7(ArrayList<Integer> ticket) {
        if (ticket.get(6) - ticket.get(0) > 19) {
            return true;
        }
        
        int diff = ticket.get(6) - ticket.get(0);
        System.out.println(diff);
        
        return false;   
    }
    
    //this method will set the ratio between odd and even numbers
    //the sum of odd and even numbers will always add up to 7 since there are
    //7 seven numbers to be drawn
    public boolean even_odd(ArrayList<Integer> ticket) {
        int countOdd = 0, countEven = 0;
        
        for (Integer nr : ticket) {
            if(nr % 2 != 0) {
                countOdd++;
            } else {
                countEven++;
            }
        }
        
        if(!(countOdd == 3 && countEven == 4)) {
            return true;
        }
        System.out.println(countOdd + " + " + countEven + " = " + (countOdd + countEven));
        
        return false;   
    }

When I'm writing

if (ticket.get(6) - ticket.get(0) > 16) {
   return true;
}

and

if(!(countOdd == 7 && countEven == 0)) {
    return true;
}

I'm expecting to get

 [1, 3, 5, 7, 9, 11, 13]
 [1, 5, 9, 11, 13, 15, 17]
 [3, 5, 9, 11, 13, 15, 17]
 ......................etc

What I get is stackoverflowerror. This seems to work fine under normal conidtions such as setting the odd/even ration to 3/4 instead of 7/0 and the range between ball nr.1 and nr.7 not too short, for instance: > 28.

Here is my whole code:

public class MainController implements Initializable {
    @FXML
    private AnchorPane anchorPane;
    
    @FXML
    private Label myMessage;
    
    @FXML
    private Button clickme;
    
    public void generateBtnClicked(ActionEvent event) {
        ArrayList<Integer> ticket = generateRandom(draw());
        String ticketString = ticket.toString();
        myMessage.setText(ticketString);
    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        
    }
    
    //this is a method for generating a random lottery ticket
    //that consist of 7 unrepeating numbers
    public ArrayList<Integer> draw() {
        ArrayList<Integer> ticket = new ArrayList<>(7); 
        Random myRandom = new Random();
        int randNR;
        
        while (ticket.size() != 7) {
            randNR = myRandom.nextInt(34) +1;
            if(ticket.isEmpty() || !ticket.contains(randNR)) {
                ticket.add(randNR);
            }
        }
        Collections.sort(ticket);
        return ticket;
    }
    
    //this is a recursive method that keeps on calling on draw method
    //until all conditions are fulfilled
    public ArrayList<Integer> generateRandom(ArrayList<Integer> ticket) {
        
        //distance/range between ball nr.1 and nr.7 condition
        if (diff1to7(ticket)) {
            return generateRandom(draw());      
        }
        
        //ratio of odd and even numbers condition
        if (even_odd(ticket)) {
            return generateRandom(draw());  
        }
        
        return ticket;
    }
    
    //this method will set the maximum allowed distance 
    //between ball nr. 1 and nr.7
    public boolean diff1to7(ArrayList<Integer> ticket) {
        if (ticket.get(6) - ticket.get(0) > 16) {
            return true;
        }
        
        int diff = ticket.get(6) - ticket.get(0);
        System.out.println(diff);
        
        return false;   
    }
    
    //this method will set the ratio between odd and even numbers
    //the sum of odd and even numbers will always add up to 7 since there are
    //7 seven numbers to be drawn
    public boolean even_odd(ArrayList<Integer> ticket) {
        int countOdd = 0, countEven = 0;
        
        for (Integer nr : ticket) {
            if(nr % 2 != 0) {
                countOdd++;
            } else {
                countEven++;
            }
        }
        
        if(!(countOdd == 7 && countEven == 0)) {
            return true;
        }
        System.out.println(countOdd + " + " + countEven + " = " + (countOdd + countEven));
        
        return false;   
    }
}

Please I need help to rewrite this code to work effectively and avoiding stackoverflowerror. I'm thinking of adding further "conditions" to this recursive method, but this needs to be fixed first.

Thanks in advance.

JT




Aucun commentaire:

Enregistrer un commentaire