简体   繁体   中英

Is there a simpler way to get multiple random values in Java?

I have created code to draw a random Oval with a random color and coordinate in a random part of a section of a plane that I have provided in swing every time my loop runs. Is there a simpler way to get these random values for the oval and if so what is it? Here is the code that I am using:

for(int i = 0; i <= 200; i++) {
            double r = Math.random();
            int k = (int)(r * 255);
            double s = Math.random();
            int l = (int)(s * 255);
            double t = Math.random();
            int m = (int)(t * 255);
            g2d.setColor(new Color(k, l, m));
            double p = Math.random();
            int o = (int)(p * 550);
            double x = Math.random();
            int y = (int)(x * 500);
            g2d.fillOval(250 + o, 250 + y, 30, 30);
        }
Random random = new Random();
for (int i = 0; i <= 200; i++) {
    g2d.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
    g2d.fillOval(250 + random.nextInt(550), 250 + random.nextInt(500), 30, 30);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM