# random number generator problem

Page 1 of 1

## 13 Replies - 4130 Views - Last Post: 16 November 2012 - 01:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299957&amp;s=badc4b405b15ddaec0f62794f6b8523e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sas786

Reputation: 0
• 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

Reputation: 1
• 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

### #3 sas786

Reputation: 0
• 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.setSize(500, 300);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);

//Adding the button, label and image to the panel
}

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();
}
}
```

### #4 jon.kiparsky

• Beginner

Reputation: 11021
• Posts: 18,804
• 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

### #5 sas786

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

## Re: random number generator problem

Posted 16 November 2012 - 10:14 AM

sure thing, wont be a minute

### #6 sas786

Reputation: 0
• 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.

### #7 jon.kiparsky

• Beginner

Reputation: 11021
• Posts: 18,804
• 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).

### #8 sas786

Reputation: 0
• 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.

### #9 sas786

Reputation: 0
• 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.

```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.setSize(500, 300);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);

//Adding the button, label and image to the panel
}

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();
}
}
```

### #10 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #11 sas786

Reputation: 0
• 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?

### #12 jon.kiparsky

• Beginner

Reputation: 11021
• Posts: 18,804
• 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.

### #13 pbl

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

Reputation: 8378
• Posts: 31,956
• 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) {

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);
}

```

### #14 jon.kiparsky

• Beginner

Reputation: 11021
• Posts: 18,804
• 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)