samedi 9 janvier 2021

math.random() doesnt create random number

I am currently having problems with the Method math.random(). I am working on a Snake programm but for some reason when i try to randomly generate the new Position for the apple the formula for generating random numbers only returns 0. I got the Formula from this website: https://www.educative.io/edpresso/how-to-generate-random-numbers-in-java

import java.util.Math;

public class Apfel {
    //Anfang Attribute 
    Position position;
    //Ende Attribute
    public Apfel(){
        setztePositionZufaellig();
    }
    public Position gibPosition(){
        return position;
    }
    
    public void setztePosition(Position pPosition){
        position = pPosition;
    }
    //this method is supposed to set a new Position for the Apple randomly.
    public void setztePositionZufaellig(){
        int min = 0;
        int max = 10;
        int randomX = (int) Math.random() * (max - min + 1) + min;
        int randomY = (int) Math.random() * (max - min + 1) + min;
        Position neu = new Position(randomX, randomY);
        position = neu;
    }

}

Any help is appreciated, thank you guys!!




Aucun commentaire:

Enregistrer un commentaire