Zufallszahl generieren mit einer linken und rechten Grenze

M3lem

Mitglied
Hallo zusammen,
Ich stehe vor folgendem Problem.
Wenn ich einen Zufallszahl zwischen 20 und 580 generieren lassen will, wird trotzdem einen Zahl über 580 generiert.
Hier ist die Klasse
Java:
import processing.core.PGraphics;

import java.util.Random;

class Ball {
    int x, y;
    int fillColor;
    float xSpeed = 5;
    float ySpeed = 5;
    int widthBound, heightBound;
    Random random = new Random();
    Ball(int fillColor, int widthBound, int heightBound) {
        this.fillColor = fillColor;
        this.widthBound  = widthBound;
        this.heightBound = heightBound;
        this.randomPos();
    }


    void randomPos() {
        this.x = (int) (Math.random()*580+20);
        this.y = (int) (Math.random()*580+20);
        System.out.println("X: "+this.x);
        System.out.println("Y: "+this.y);
    }
    void draw(PGraphics g) {
        g.fill(this.fillColor);
        g.ellipse(this.x, this.y, 40, 40);
        x += xSpeed;
        y += ySpeed;


        if (x < 20 || x > widthBound-20) {
            xSpeed *= -1;

        }

        if (y < 20  || y > heightBound-20) {
            ySpeed *= -1;

        }
    }
}

Hier sind einige Ausgaben:
Code:
X: 208
Y: 442
X: 426
Y: 535
X: 421
Y: 593 // das ist die Zahl, die über 580 ist

Und das ist die Main-Methode:
Code:
import processing.core.PApplet;
public class Billiardtisch extends PApplet {
    public static void main(String[] args) {
        PApplet.runSketch(new String[]{""}, new Billiardtisch());
    }

    Ball[] balls = new Ball[100];
    public void settings() {
        super.size(600, 600);
    }
    public void setup() {
        frameRate(20);
        super.background(0);
        for(int i = 0; i < balls.length; i++) {
            balls[i] = new Ball((color((int) (Math.random()*(255)),(int) (Math.random()*(255)),(int) (Math.random()*(255)))),super.width,super.height);
        }
    }
    public void draw() {
        super.background(0,102,51);
        for (Ball elem : balls) elem.draw(super.g);
    }
}
 
this.x = (int) (Math.random()*580+20);
hier entsteht eine Zufallszahl im Intervall [0..580[, zu der nochmals 20 hinzu addiert wird. Damit ergibt sich eine Range von 20 <= x < 600

P.S.: wenn Du schon eine Instanz der Klasse Random initialisierst, wieso nutzt Du sie dann nicht?
Random random = new Random();
Dabei würde folgendes ganau das gewünschte ergebnis liefern.
Java:
this.x = random.nextInt(20, 580); // falls 580 mit dabei sein soll -> (20, 581), denn die Obergrenze ist immer Exklusive
 
hier entsteht eine Zufallszahl im Intervall [0..580[, zu der nochmals 20 hinzu addiert wird. Damit ergibt sich eine Range von 20 <= x < 600

P.S.: wenn Du schon eine Instanz der Klasse Random initialisierst, wieso nutzt Du sie dann nicht?

Dabei würde folgendes ganau das gewünschte ergebnis liefern.
Java:
this.x = random.nextInt(20, 580); // falls 580 mit dabei sein soll -> (20, 581), denn die Obergrenze ist immer Exklusive
Ich wusste nicht, dass man mit random.nextInt() die untere Grenze auch festlegen könnte.
Ich dachte, dass es nur um die obere Grenze geht und man dazu die untere Grenze addiert, aber jetzt hat weiß ich Bescheid, danke schön
 

Zurück
Oben