# Problems with Math.Random() in a for loop

• September 27th, 2012, 03:23 PM
csharp100
Problems with Math.Random() in a for loop
Hello everyone,
I have a program here that will display between 2 -4 cars "racing" accross the track. I am trying to get the cars to move at random speeds. Some faster, some slower but always different. I have tried a for loop and if it is working it is so neglible I cannot tell the difference. Here is the code I did for the loop:
Code :

```public CarImage() { int y = (int)(Math.random() * 10) + 10; Timer timer1 = new Timer(y, new ActionListener(){ public void actionPerformed(ActionEvent e) { x += 10; c ++; repaint(); } }); timer1.start(); }```

Here is the complete code in case someone needs to see what is going on:
Code :

```import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random;   public class RacingCar extends JFrame {   public RacingCar() { int x = (int)(Math.random() * 3) + 2; setLayout(new GridLayout(x, 1, 5,5)); for (int i = 0; i < x; i++){ add(new CarImage()); } }   public static void main(String[] args) { JFrame frame = new RacingCar(); frame.setTitle("Racing Car"); frame.setSize(1200, 350); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }   class CarImage extends JPanel { protected int x = 0; protected int y = 150; protected int z = 300; protected int c = 0;   public CarImage() { Timer timer1 = new Timer(40, new ActionListener(){ public void actionPerformed(ActionEvent e) { x += 10; c ++; repaint(); } }); timer1.start(); }   public void paintComponent(Graphics g) { super.paintComponent(g); // x = 0; y = getHeight(); z = getWidth(); g.setColor(Color.WHITE); g.fillRect(0, 0, z, y); Polygon polygon = new Polygon(); polygon.addPoint(x + 10, y - 21); polygon.addPoint(x + 20, y - 31); polygon.addPoint(x + 30, y - 31); polygon.addPoint(x + 40, y - 21);   if (x < z - 50) { g.setColor(Color.BLACK); g.fillOval(x + 10, y - 11, 10, 10); g.fillOval(x + 30, y - 11, 10, 10); g.setColor(Color.BLUE); g.fillRect(x, y - 21, 50, 10); g.setColor(Color.GRAY); g.fillPolygon(polygon); g.setColor(Color.RED); } else x = 0; if (c < z - 86) g.drawString("Clint's Car", c, y - 51); else c = 0;   }   } }```

Please note that the loop I tried is NOT in the above program.

If anyone has a suggestion as to how I can get the different speeds I could use some help. A snippet of code would be nice. Yes this is homework. I did try what I thought would work but obviously it did not work.
• September 27th, 2012, 03:35 PM
newbie
Re: Problems with Math.Random() in a for loop
As Math.random() only produces a value between 0.0 and 1.0, the highest value you can expect by multiplying by 10 is 10, and can be as low as 0.
As you're then using that variable to control the timer, a difference of 0-10 MILLISECONDS, yes milliseconds is what javax.Timer uses (1000 = 1 second) is hardly going to make any difference at all to the car speed, especially since the amount each car moves horizontally is defined by a constant.

Edit: Just noticed the discrepancy in the single code stub up top compared with the code in the complete class. Which is which? Seems my above answer is pointless in that case.
The way you've designed the cars really isn't the best way of controlling movement etc, but if you insist, you might just consider generating a random number within a range inside the javax.Timer and adding that to the X variable instead of a constant speed.
• September 27th, 2012, 03:51 PM
csharp100
Re: Problems with Math.Random() in a for loop
Quote:

Edit: Just noticed the discrepancy in the single code stub up top compared with the code in the complete class. Which is which? Seems my above answer is pointless in that case.
Yes there is a discrepancy. I noted that right below the full code.
• September 27th, 2012, 04:05 PM
newbie
Re: Problems with Math.Random() in a for loop
Quote:

Please note that the loop I tried is NOT in the above program.
I saw that yes, and of course wondered why not ,but I also didn't expect you to post two irrelevant versions :D
Good luck.
• September 27th, 2012, 06:18 PM
Zaphod_b
Re: Problems with Math.Random() in a for loop
I don't see any basically wrong with the concept. It's a matter of tuning the parameters, I'm thinking.

Why not experiment with random values that are more spread out (i.e. a greater range)? Make it show what it's using.
Maybe something like
Code java:

``` public CarImage() { int y = (int)(Math.random() * 20) + 1; // 1..20 Smaller is faster . . x += 1; // Smaller slows everything down . . . }); timer1.start(); System.out.println("y = " + y); // Make it show what it is using for the Timer parameter } // End CarImage()```

For purposes of deciding on the best range for the random stuff, you could even temporarily make the timing stuff get picked up from command line arguments so that you could experiment and "fine tune" heuristically without recompiling. (Although with this small program re-compiling isn't much of a hardship.)

Cheers!

Z