# Thread: Lottery help, looping problem

1. Member
Join Date
Oct 2010
Posts
3
Rep Power
0

## Lottery help, looping problem

I'm new to these forums :( So I don't know the format that I should be posting questions in so I'm sorry if I post incorrectly.

Anyway, I'm making a lottery program for class similar to lotto 6/49 in Ontario, Canada. So far I have made it so that you enter your numbers, you pick how many numbers you want it to match, and then it picks non-repeating numbers from 1-49 and compares them.

Now it doesn't really compare them the way I want it to. What's happening is that I will pick the numbers and what will happen is it will keep on looping and spit out sets of 6 random numbers and compare them, the problem is, is that the computer is comparing ALL the random numbers, even the ones from the previous sets of random numbers. For example I pick: 1,2,3,4,5,6 and want to match 3 numbers, so I press go. Now I can see in the system output that the draws are:
23
35
45
32
24
31

40
29
37
6
13
34

1
33
4
25
42
18

As you can see it is matching numbers from previous draws making all my draws take only a maximum of 5 sets to match which is unrealistic.

Here is my code for the loop:

Java Code:
```  {
if(e.getSource()==button1)
for(int g=0;g<6;g++)
{
entries[g]=Integer.parseInt(t[g].getText());
}
numbertomatch=Integer.parseInt(matchnumber.getText());
{
while(numbertomatch>matches)
{
{
for(int m=0;m<6;m++)
{
do
{
x=(int)(Math.random()*49)+1;
}while(checklist[x]==1);

d[m].setText(""+x); //placing RN into a Label
draws[m]=x; //storing RN to appropriate
checklist[x]=1;
tries++;
System.out.println(x);

for(int q=0;q<6;q++)
{
for(int u=0;u<6;u++)
{
if(draws[u]==entries[q])
{
matches++;
}
}
}
draws[m]=0;
plays=new JLabel ("It took "+ tries +" plays to match.");
}
}
}
}
}```

I also wanted it to count how many times it took to match the numbers so I used the variable "tries" to count that, but it isn't counting. I think the problems are related. Any help is welcome, and here is my entire code if you'd like to try it out.

Java Code:
```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class nonrigged649 extends JFrame implements ActionListener{
int x=0, matches=0, numbertomatch=0, tries=0;
int []checklist;
int []entries;
int []draws;
JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
JButton button1;
JTextField []t;
JTextField matchnumber;
JLabel title, tomatch, plays;
JLabel []d;

public static void main(String[ ] args)
{
new nonrigged649 ();
}

public nonrigged649 (){
panel1=new JPanel();
panel1.setLayout(new BorderLayout());
panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel3=new JPanel();
panel3.setLayout(new GridLayout(1,6));
panel4=new JPanel();
panel4.setLayout(new GridLayout (1,6));
panel5=new JPanel();
panel5.setLayout(new BorderLayout());
panel6=new JPanel();
panel6.setLayout(new GridLayout (2,1));
panel7=new JPanel();
panel7.setLayout(new GridLayout (2,1));
panel8=new JPanel();
panel8.setLayout(new BorderLayout());

matchnumber=new JTextField();
plays=new JLabel ("It took "+ matches +" plays to match.");
title=new JLabel("Lotto 6/49");
tomatch=new JLabel("How many numbers to match?");

button1=new JButton ("GO");

draws= new int[6];
entries= new int[6];
d=new JLabel[6];
for(int a=0;a<6;a++)
{
d[a]=new JLabel (" ");

}
title=new JLabel("Lotto 6/49");

t=new JTextField[6];
for(int b=0;b<6;b++)
{
t[b]=new JTextField ("");

entries[b]=0;
}

checklist=new int[50];

this.setSize(200,200);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
for(int g=0;g<6;g++)
{
entries[g]=Integer.parseInt(t[g].getText());
}
numbertomatch=Integer.parseInt(matchnumber.getText());
{
while(numbertomatch>matches)
{
{
for(int m=0;m<6;m++)
{
do
{
x=(int)(Math.random()*49)+1;
}while(checklist[x]==1);

d[m].setText(""+x); //placing RN into a Label
draws[m]=x; //storing RN to appropriate
checklist[x]=1;
tries++;
System.out.println(x);

for(int q=0;q<6;q++)
{
for(int u=0;u<6;u++)
{
if(draws[u]==entries[q])
{
matches++;
}
}
}
draws[m]=0;
plays=new JLabel ("It took "+ tries +" plays to match.");
}
}
}
}
}
}```
Last edited by Fubarable; 10-31-2010 at 04:46 PM. Reason: Moderator Edit: code tags added

2. Member
Join Date
Oct 2010
Posts
3
Rep Power
0

3. Member
Join Date
Oct 2010
Posts
3
Rep Power
0
Ok I figured out that I need to set matches to 0 before every loop to solve my problem. My only problem now is that I don't know how to reset checklist without breaking its function, which is to prevent the random number from repeating. I don't know if this forum is dead but anyone out the help me please :(

#### Posting Permissions

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