# Random number generation

• 07-05-2013, 11:17 AM
vastrolorde
Random number generation
i have this function
Code:

```public double randoms(int g){                 return (random.nextInt(2*g) - g);                 }```
in theori it shoudl generate numbers from -g to g. But it only generates negative numbers.
• 07-05-2013, 11:26 AM
DarrylBurke
Re: Random number generation
Are you sure?

db
• 07-05-2013, 11:30 AM
gimbal2
Re: Random number generation
No it doesn't. Code (DISCLAIMER: lazy test code I wouldn't write for a real application)

Code:

```import java.util.Random;   public class Test2 {   private static Random random = new Random();     public static double randoms(int g){         return (random.nextInt(2*g) - g);   }      public static void main (String [] args){           while(true){           System.out.println("NUM: " + randoms(10));         }   } }```
Output:

Code:

```NUM: 9.0 NUM: 5.0 NUM: -10.0 NUM: -4.0 NUM: -8.0 NUM: -5.0 NUM: -4.0 NUM: 3.0 NUM: -5.0 NUM: -8.0 NUM: -10.0 NUM: 7.0 NUM: -10.0 NUM: -1.0 NUM: 6.0 NUM: -10.0```
• 07-05-2013, 02:00 PM
vastrolorde
Re: Random number generation
Ok. But when i use it with my particle test code, all the particles are drawn in 1 location. its the upper left quater.
Code:

```for(int i = 0; particle.size() < 100; i++){                         double x = randoms(40) +100;                         double y = randoms(40) +100;                         Point2D.Double test = new Point2D.Double(x,y);                         if(raadius.contains(test)){                                 double x2 = x - mid.getX();                                 double y2 = y - mid.getY();                                 if(x2 < 0) x2 = x2 * -1;                                 if(y2 < 0) y2 = y2 * -1;                                 if(x < mid.getX() && y < mid.getY()){                                         dx = (x2 < y2)? x2 / y2 : 1;                                         dy = (y2 < x2)? y2 / x2 : 1;                                 }                                 else if(x < mid.getX() && y > mid.getY()){                                         dx = (x2 < y2)? x2 / y2 : 1;                                         dy = (y2 < x2)? (y2 / x2)*-1 : -1;                                         }                                 else if(x > mid.getX() && y < mid.getY()){                                         dx = (x2 < y2)? (x2 / y2)*-1 : -1;                                         dy = (y2 < x2)? y2 / x2 : 1;                                         }                                 else{                                         dx = (x2 < y2)? (x2 / y2)* -1 : -1;                                         dy = (y2 < x2)? (y2 / x2)* -1 : -1;                                 }                                                                 particle.add(new Particle(x,y,dx,dy,0,0,1,1000));                                 }                         }```
Im using this code to fill the array with the particle locations.
• 07-05-2013, 02:17 PM
gimbal2
Re: Random number generation
Well that's a whole different problem than random number generation, isn't it?

Perhaps that Particle constructor isn't correct and it actually does not assign the values properly? Who knows - you'll have to do some debugging. Try painting only one particle and System.out.println()-ing all the different values that you have. One or more are not what you assume them to be.
• 07-05-2013, 02:38 PM
vastrolorde
Re: Random number generation
Fixed it :D in the random generation part it needs to be 80 not 40 and then it works perfectly :D