13 Replies - 1909 Views - Last Post: 16 November 2012 - 01:29 PM Rate Topic: -----

#1 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

random number generator problem

Posted 13 November 2012 - 05:16 AM

I have become a little stuck with one program that i am trying to create which is a random number generator for 6 numbers. Ive done the core code so far which covers the algorithms, but i am confused on how to add code which i think covers the gui aspect of the program.

I am still a newbie to java, however i managed to do the main algorithms as i did C++ and the programming language is very similar. I havent been taught how to create the gui aspects very well and every search i put into google doesnt make sense to me.

The bit im stuck on states the class should display a logo, a button and a text box to contain the six numbers. The numbers should only be generated when the button is pressed.

This is my code so far

import java.util.*;


public class LotteryNumbers
{
    int[] LotteryNumbers = new int[49];
    int i;
    Random rgen = new Random();  
    
    //Creating an Array of 50 integers
     public void createNumbers()  
    {
        for (i=0; i < LotteryNumbers.length; i++) 
        {   
            LotteryNumbers[i] = i + 1;
        }
    }

    //Shuffling the numbers in the array
    public void shuffleNumbers()
    {
        for (int j=0; j < LotteryNumbers.length; i++) 
        {
            int randomPosition = rgen.nextInt(LotteryNumbers.length);
            int temp = LotteryNumbers[j];
            LotteryNumbers[j] = LotteryNumbers[randomPosition];
            LotteryNumbers[randomPosition] = temp;
        }
    }
    
    //Sorting the numbers in the array
    public void sortNumbers()
    {
        for(int i=0; i < LotteryNumbers.length-1; i++) 
        {
            for(int j=0; j < LotteryNumbers.length-1-i; j++) 
            {
                if(LotteryNumbers[j] > LotteryNumbers[j+1]) 
                {
                    int temp = LotteryNumbers[j];
                    LotteryNumbers[j] = LotteryNumbers[j+1];
                    LotteryNumbers[j+1] = temp;
                }
            }
        }
    }
    
    // Printing the numbers out
    public void printnumbers( int j, int [] LotteryNumbers)
    {
        for (j = 0; j<= 6 && j >= 1; j++)
        {
            System.out.println("Lotto number: " + j + ":" + LotteryNumbers);
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: random number generator problem

#2 daarkfall  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 27-August 09

Re: random number generator problem

Posted 13 November 2012 - 05:46 AM

Hi sas,

Java ui development can be troublesome to get the hang of but essentially what you will want to do is follow this guide
http://docs.oracle.c...torial/uiswing/ .

For the sake of completeness i would change
public void printnumbers( int j, int [] LotteryNumbers)

to

public int[] printnumbers( int j, int [] LotteryNumbers)


This means you can call it from your front end and receive your lottery numbers for display.

Good luck

~Daark
Was This Post Helpful? 0
  • +
  • -

#3 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 10:04 AM

Ok, i have managed to do 95% of what i needed. All the GUI and coding is in place, however whenever i run the programme, it should display the 6 lotto numbers in the label but it doesnt. Instead it gives out a horrible box of code which i have no clue as to what it is.

I am currently using Bluej IDE.

import java.util.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 

public class LotteryGenerator implements ActionListener

{       
        private JFrame frame = new JFrame("Lottery Generator Program");
        private JPanel panel = new JPanel(); 
        private JButton GenerateLottoNums = new JButton("Generate Numbers");
        private String lottoNums = "";
        private JTextField LotteryText = new JTextField(50);
        ImageIcon Lottery = new ImageIcon("Lottery.png");
        private JLabel label = new JLabel(Lottery);
             
        
        public static void main (String[] args)
        {
            LotteryGenerator gui = new LotteryGenerator();
        }
        
        public void LotteryGenerator()
        {
           //Defining the Window
            frame.add(panel);
            frame.setSize(500, 300);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            GenerateLottoNums.addActionListener(this);
           
            //Adding the button, label and image to the panel
            panel.add(label);
            panel.add(GenerateLottoNums);
            panel.add(LotteryText);
        }
        
        int[] LotteryGenerator = new int[49];
        int i = 0;
    
        //Creating an Array of 50 integers
        public void createNumbers()  
        {
            for (i=0; i < LotteryGenerator.length; i++) 
            {   
                LotteryGenerator[i] = i + 1;
            }
        }

        //Shuffling the numbers in the array
        public void shuffleNumbers()
        {
            int ShuffleLength; 
            int Index; 
            int Position;
            ShuffleLength = LotteryGenerator.length;
            for (int j=0; j < LotteryGenerator.length; j++) 
            {
               Index = (int) (Math.random() * ShuffleLength);
               Position = LotteryGenerator[Index];
               System.out.println(Position + ":" + LotteryGenerator[Position]);
               LotteryGenerator[Index] = LotteryGenerator[--ShuffleLength];
            }
        }
    
        //Sorting the numbers in the array
        public void sortNumbers()
        {
            for(int i=0; i < LotteryGenerator.length-1; i++) 
            {
                for(int j=0; j < LotteryGenerator.length-1-i; j++) 
                {
                    if(LotteryGenerator[j] > LotteryGenerator[j+1]) 
                    {
                        int temp = LotteryGenerator[j];
                        LotteryGenerator[j] = LotteryGenerator[j+1];
                        LotteryGenerator[j+1] = temp;
                    }
                }
            }        
                       
        }
    
        // Printing the numbers out
        public void showNums()
        {
            for (int j = 0; j < 6; j++)
            {
                lottoNums += " " + LotteryGenerator[j] + " ";
            }
            LotteryText.setText(lottoNums);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            createNumbers();
            shuffleNumbers();
            sortNumbers();
            showNums();
        }
}

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: random number generator problem

Posted 16 November 2012 - 10:12 AM

Just 'cause I'm lazy and not going to build and run your code... tell me more about your "horrible" result. Can you paste us the text that you're seeing?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 10:14 AM

sure thing, wont be a minute
Was This Post Helpful? 0
  • +
  • -

#6 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 10:19 AM

This is what i get once i hit the generate button.


Attached Image
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: random number generator problem

Posted 16 November 2012 - 10:42 AM

Okay, that's a stack trace. What it's telling you is that at line 67 of your program, you tried to access element 49 of some array that doesn't have an element 49. This happens in the shuffleNumbers method, and that method was called by the actionPerformed method (line 103)

So that gives you a lot of clues about what's going on here. Let's take a look.

I can see that your line number in the posted code are not in sync with what your compiler is reading - that happens. But I'm willing to bet that when you go back and look, you'll find that this is the problem line:

System.out.println(Position + ":" + LotteryGenerator[Position]);



Whats "Position"?

It's this:
Position = LotteryGenerator[Index];


Are you sure that all of the numbers in the LotteryGenerator array are less than LotteryGenerator.length? I think that they're not.

While you're fixing that, you should probably fix your variable names to Java coding standards - initial uppercase is used for class names, use camelCase for variables (including object references).
Was This Post Helpful? 1
  • +
  • -

#8 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 10:55 AM

Il play around with it and see what i can come up with.

Thanks for the quick replys
Was This Post Helpful? 0
  • +
  • -

#9 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 11:56 AM

Ok, i have made a few changes to my code and it now produces some numbers in the label. The only problem is that is keeps repeating numbers 1,2,3,4,5,6 and for some reason it is displaying it to the far left and half the numbers are chopped off.

Ive been working on this for the past four days and i think im beginning to lose it as im sure its something small that could be easily corrected.

Any corrections would be much appreciated.

Attached Image

import java.util.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 

public class lotteryGenerator implements ActionListener

{       
        private JFrame frame = new JFrame("Lottery Generator Program");
        private JPanel panel = new JPanel(); 
        private JButton generateLottoNums = new JButton("Generate Numbers");
        private String lottoNums = "";
        private JTextField lotteryText = new JTextField(50);
        ImageIcon Lottery = new ImageIcon("Lottery.png");
        private JLabel label = new JLabel(Lottery);
             
        
        public static void main (String[] args)
        {
            lotteryGenerator gui = new lotteryGenerator();
        }
        
        public void lotteryGenerator()
        {
           //Defining the Window
            frame.add(panel);
            frame.setSize(500, 300);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            generateLottoNums.addActionListener(this);
           
            //Adding the button, label and image to the panel
            panel.add(label);
            panel.add(generateLottoNums);
            panel.add(lotteryText);
        }
        
        int[] lotteryGenerator = new int[50];
        int i = 0;
        Random rgen = new Random();
        
        //Creating an Array of 50 integers
        public void createNumbers()  
        {
            //Creating an Array of 50 Integers            
            for (i=0; i < lotteryGenerator.length; i++) 
            {   
                lotteryGenerator[i] = i + 1;
            }
        }
        
        public void shuffleNumbers()
        {
            // Shuffling the numbers in the array
            for (int i=0; i < lotteryGenerator.length; i++) 
            {
                int randomPosition = rgen.nextInt(lotteryGenerator.length);
                int temp = lotteryGenerator[i];
                lotteryGenerator[i] = lotteryGenerator[randomPosition];
                lotteryGenerator[randomPosition] = temp;
            }           
        }     
    
        //Sorting the numbers in the array
        public void sortNumbers()
        {
           for(int i=0; i < lotteryGenerator.length-1; i++) 
            {
               for(int j=0; j < lotteryGenerator.length-1-i; j++) 
                {
                    if(lotteryGenerator[j] > lotteryGenerator[j+1]) 
                    {
                       int temp = lotteryGenerator[j];
                       lotteryGenerator[j] = lotteryGenerator[j+1];
                       lotteryGenerator[j+1] = temp;
                    }
                }
            }        
                       
        }
    
        // Printing the numbers out
        public void showNums()
        {
            for (int j = 0; j < 6; j++)
            {
                lottoNums += " " + lotteryGenerator[j] + " ";
            }
            lotteryText.setText(lottoNums);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            createNumbers();
            shuffleNumbers();
            sortNumbers();
            showNums();
        }
}

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: random number generator problem

Posted 16 November 2012 - 12:06 PM

new JTextField(50);

is just to big... if you show 6 digits + 1 space per digit make it nwe JTextField(14);

Add a space before when you start

Or ven better use a JLabel rather than a JTextField ad set it Centered
Was This Post Helpful? 0
  • +
  • -

#11 sas786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-November 12

Re: random number generator problem

Posted 16 November 2012 - 12:23 PM

thats perfect, thanks for your help.

any ideas why i keep getting the same numbers 1,2,3,4,5,6 everytime i press generate?

Attached Image
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: random number generator problem

Posted 16 November 2012 - 12:26 PM

You can probably answer that. Start by looking at what you're putting into your JTextField. Clearly, the value of that is 1,2,3,4,5,6. (because that's what you're seeing). How does it get that way? Where does that content come from?
Try to trace back from where the error becomes obvious.
Was This Post Helpful? 1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: random number generator problem

Posted 16 November 2012 - 12:56 PM

Shuffling the array is kind of useless
Put your numbers in an ArrayList and remove randomly from it

    ArrayList<Integer> al = new ArrayList<Integer>();
    for(int i = 1; i < 50; ++i) {
        al.add(i);

    Random ran  = new Random();
    int[] win = new int[6];
    for(int i = 0; i < win.length; ++i) {
       int index = ran.nextInt(al.size());
       win[i] = al.remove(index);
    }


Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: random number generator problem

Posted 16 November 2012 - 01:29 PM

Shuffling the array and then sorting it is especially pointless. :)

However, since we're on the topic, you might want to look at the Knuth shuffle. It's well explained at this blog post and Sedgewick points to another article which goes into more detail, but I can't put my hands on that one at the moment. In a nutshell, it describes a correct procedure for shuffling an array. pbl's random selection from an arraylist is simpler to implement, and should work equally well, since the arraylist is a shrink-to-fit data structure. (in fact, you can convince yourself that the arraylist method works by using a deck of cards and some source of randomness)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1