Results 1 to 4 of 4
  1. #1
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default util.random help.

    this is part of the program, purpose of program is to generate 10 vehicles randomly. For some reasons, my random always equals 0. can some one point out my problem?

    Java Code:
    import java.util.Random;
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Vehicle extends JPanel{ 
    public Vehicle(){
    
    
    }
    public void RandomGen(){
    	Random r=new Random();
    	z=r.nextInt(1);
    	}
    public int getNumber(){
    	
    	return z;
    	}
    
    
    
    
    public int z;
    }



    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    
    
    public class TempTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    	JFrame frame=new JFrame();
    	frame.setTitle("Cars and Trucks");
    	frame.setSize(600, 600);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	Container pane=frame.getContentPane();
    	pane.setLayout(new GridLayout(1,1));
    	
    	Vehicle v=new Vehicle();
    	v.RandomGen();
    	int z=v.getNumber();
    	
    
    	if(z==0){
    		Car c=new Car();
    		pane.add(c);
    		}
    		else {
    			Truck t=new Truck();
    			pane.add(t);
    			}
    	System.out.println(z);
    	
    	frame.setVisible(true);
    	
    
    }
    	
    	}
    Last edited by Fubarable; 11-18-2011 at 06:30 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: util.random help.

    Moderation: post "approved" and is now visible. Also code tags added to aid posted code's readability.

    To the OP: to be blunt, your indentation style is atrocious which makes your code hard to read and harder to understand. You will likely get better and faster help if you pay attention to correcting this and towards using code tags when posting code here in the future.

    Luck!

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: util.random help.

    Habituate x to y

    something.nextInt(y-x+1)x;

    In your case you want to habituate a number between 1 and 10 include, then x = 1 and y = 10
    Then it should be somthing.nextInt(10-1+1)+1;
    Last edited by tnrh1; 11-18-2011 at 07:23 PM.

  4. #4
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: util.random help.

    thanks for the help, i got it to work. i was in a rush to school before posting this. Ill make it look nicer next time.
    Last edited by fishy8158; 11-18-2011 at 10:26 PM.

Similar Threads

  1. Random Generation Not being random
    By Mmarzex in forum New To Java
    Replies: 8
    Last Post: 08-04-2011, 10:53 PM
  2. Replies: 3
    Last Post: 10-24-2010, 10:24 PM
  3. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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