Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Random number generation

  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Random number generation

    Are you sure?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,843
    Rep Power
    5

    Default 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

  4. #4
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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);
    			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.
    Last edited by vastrolorde; 07-05-2013 at 02:04 PM.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,843
    Rep Power
    5

    Default 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.
    DarrylBurke likes this.

  6. #6
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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

Similar Threads

  1. Random World Generation like terraria?
    By totalspelnerd in forum Java Gaming
    Replies: 6
    Last Post: 03-02-2013, 06:31 PM
  2. Random shape generation
    By zeeextra in forum New To Java
    Replies: 0
    Last Post: 12-04-2009, 05:57 AM
  3. Trouble with Random number generation
    By SteroidalPsycho in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 11:15 PM
  4. Random number generation
    By toasty in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 11:41 AM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 AM

Posting Permissions

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