Thread: Random number generation

1. Senior Member Join Date
Jan 2013
Posts
124
Rep Power
0 Random number generation

i have this function
Java 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.  Reply With Quote

2. Re: Random number generation

Are you sure?

db  Reply With Quote

3. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12 Re: Random number generation

No it doesn't. Code (DISCLAIMER: lazy test code I wouldn't write for a real application)

Java 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:

Java 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  Reply With Quote

4. Senior Member Join Date
Jan 2013
Posts
124
Rep Power
0 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.
Java 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);
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;
}

}
}
Im using this code to fill the array with the particle locations.
Last edited by vastrolorde; 07-05-2013 at 02:04 PM.  Reply With Quote

5. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12 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.  Reply With Quote

6. Senior Member Join Date
Jan 2013
Posts
124
Rep Power
0 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  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•