Results 1 to 3 of 3
  1. #1
    Mr U is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Random number not gen. number 0???

    So I'm really getting ticked I've looked around on the web for some help and everything say's I'm doing everything the right way. Yet when I'm trying to test this it output's a 0

    Java Code:
    {
    				int id;
    				int id2;
    				Random ran = new Random();
    				String base = "L 1147166230";
    				id = ran.nextInt(4 + 1); //  error here not suppose to gen number 0
    				id2 = ran.nextInt(999);
    				Val = base + id + id2;
    				box.setText(Val);
    				box.setHorizontalAlignment(JTextField.CENTER);
    				
    			}

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Random number not gen. number 0???

    i ran your code and it worked fine..
    maybe it's about how you define the box object..

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Random number not gen. number 0???

    From the API:

    Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

    If you pass 5 to the nextInt method then the possibly return values will be 0, 1, 2, 3 & 4. If you do not want zero being generated then you will need to adjust your code.

Similar Threads

  1. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  2. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  3. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM
  4. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  5. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 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
  •