Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Random Number Generator Always gives 0

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Number Generator Always gives 0

    Why does it give me a 0 when my range is specifically 1~56?

    Code:
     //**********random()***************
            public static int random(int a, int b)
            {
                 return((int)((b - a + 1)*Math.random() + a));
            }
     //**********superLottoNumbers()*********
            public static int[] superLottoNumbers()
            {
                int numZ[] = new int[6];
                for(int h = 0; h <=4; h++)
                {
                    numZ[h] = random(1,47);
                }
                Arrays.sort(numZ);
                numZ[5] = random(1,27);
                return(numZ);
            }
         //**********megaMillionNumbers()*********   
            public static int[] megaMillionNumbers()
            {
                int numZ[] = new int[6];
                for(int i = 0; i <= 4; i++)
                {
                    numZ[i] = random(1,56);
                }
                Arrays.sort(numZ);
                numZ[5] = random(1,46);
                return(numZ);
            }
        public static void main(String args[])
            {
                if(commandLine1(args[0]) == false) System.out.println("Not a valid type of Lottery");
                //if(commandLine2(args[1]) == false) System.out.println("Not a valid number of tickets");
                if(args[0].equalsIgnoreCase("sl"))
                {
                    int num[] = new int[6];
                    num = superLottoNumbers();
                    for(int h: num) System.out.print(h+" ");
                }
                if(args[0].equalsIgnoreCase("mm"))
                {
                    int num[] = new int[6];
                    num = megaMillionNumbers();
                    for(int i: num) System.out.print(i+" ");
                }
            }
    }

    HELP ASAP. THanks
    Last edited by tlckl3m3elmo; July 14th, 2011 at 04:26 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Random Number Generator Always gives 0

    When you compile your code, you will notice it's the first number which is always 0.
    All the others are random and within the correct range.

    It's deceiving though as the Arrays.sort(numZ); makes it the first one.
    So in reality, it looks like the last number is always printed as 0.

    With this in mind, it looks as though the last value of the array isn't being populated.

    Which leads us to the part of the code which populates the array.

    Take a look at:

    		for (int i = 0; i <= 4; i++) {
    			numZ[i] = random(1, 56);
    		}

    The problem is your for loops. See if you can figure out what it is

    In future, my advice is to go back through your code and break it down into smaller parts. Get each part working correctly and make sure you understand what is happening before moving on.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Number Generator Always gives 0

    Why don't you just:
    int number = 1+new Random().nextInt(56);

    Seems alot simpler, doesn't it?

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  3. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 11:00 AM
  4. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM