# Problem in stoping the rpeating number

• 01-03-2006, 09:25 AM
MmG4EvER
Problem in stoping the rpeating number
import BreezySwing.*;
import javax.swing.*;
import java.awt.*;

public class Lottery extends GBFrame{

private JLabel ALbl, BLbl, CLbl, DLbl, ELbl, FLbl, SLbl;
private IntegerField AIf, BIf, CIf, DIf, EIf, FIf, SIf;
private JButton startBtn, clearBtn;

public Lottery(){
// Lables
ALbl = addLabel("A", 1, 1, 1, 1);
BLbl = addLabel("B", 1, 2, 1, 1);
CLbl = addLabel("C", 1, 3, 1, 1);
DLbl = addLabel("D", 1, 4, 1, 1);
ELbl = addLabel("E", 1, 5, 1, 1);
FLbl = addLabel("F", 1, 6, 1, 1);
SLbl = addLabel("S", 1, 7, 1, 1);

//Intger Fields
AIf = addIntegerField(0, 2, 1, 1, 1);
BIf = addIntegerField(0, 2, 2, 1, 1);
CIf = addIntegerField(0, 2, 3, 1, 1);
DIf = addIntegerField(0, 2, 4, 1, 1);
EIf = addIntegerField(0, 2, 5, 1, 1);
FIf = addIntegerField(0, 2, 6, 1, 1);
SIf = addIntegerField(0, 2, 7, 1, 1);
SIf.setBackground(new Color(255, 255, 0));

//Buttons
startBtn = addButton("Start", 3, 1, 2, 1);
clearBtn = addButton("Clear", 3, 4, 2, 1);

// Size && visiblity && Title
setTitle("Lottery Number Generator By >>MujiCom<<");
setSize(500, 250);
setVisible(true);
}

public void buttonClicked(JButton btn){

if(btn == startBtn){
AIf.setNumber((int)(Math.random() * 98 + 1));
BIf.setNumber((int)(Math.random() * 98 + 1));
CIf.setNumber((int)(Math.random() * 98 + 1));
DIf.setNumber((int)(Math.random() * 98 + 1));
EIf.setNumber((int)(Math.random() * 98 + 1));
FIf.setNumber((int)(Math.random() * 98 + 1));
SIf.setNumber((int)(Math.random() * 98 + 1));

}else if(btn == clearBtn){
AIf.setNumber(0);
BIf.setNumber(0);
CIf.setNumber(0);
DIf.setNumber(0);
EIf.setNumber(0);
FIf.setNumber(0);
SIf.setNumber(0);
}

}

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

Iwould like to add a code that will not allow the programe to rpeat the numbers like A != B and B != C and C != D and ....
• 01-03-2006, 11:34 AM
nspils
Well, using randomly generated numbers does not assure you that there will be no repeats.

You will get what are supposed to be "better" random numbers by using java.util.Random -

Random r = new Random();
AIf = r.nextInt();
BIf = r.nextInt();
etc.

if you want to limit to a range, such as 0-99, use modulo

AIf = r.nextInt() % 100;
• 01-03-2006, 11:53 AM
MmG4EvER
it says
C:\Documents and Settings\Mujtaba\Desktop\Lottery.java:57: incompatible types
found : int
required: BreezySwing.IntegerField
AIf = r.nextInt() % 100;
• 01-03-2006, 03:24 PM
nspils
You have an "IntegerField" and r.nextInt() returns an int. How are you converting from an int to an IntegerField? From your code it appears that BreezySwing.IntegerField has a setNumber() method - so might you use AIf.setNumber( r.nextInt() )?
• 01-03-2006, 06:08 PM
MmG4EvER
it works but it genrates negtiver numbers not btween 0-99 +

public void buttonClicked(JButton btn){

Random r = new Random();

if(btn == startBtn){
AIf.setNumber( r.nextInt() % 100);

BIf.setNumber( r.nextInt() % 100);

CIf.setNumber( r.nextInt() % 100);

DIf.setNumber( r.nextInt() % 100);

EIf.setNumber( r.nextInt() % 100);

FIf.setNumber( r.nextInt() % 100);

SIf.setNumber( r.nextInt() % 100 );
• 01-03-2006, 06:29 PM
nspils
The Java implementation of the modulo operator has an inadequate implementation of modulo when it comes to negative numbers (the manner in which it works with 2's complement is incomplete). If the random number is negative, the formula

(n % 100) + 100;

will give the true "remainder" you are looking for.
• 01-04-2006, 03:02 AM
MmG4EvER
thx for all the help

just now the totur of the subject said that he made a mistake and the number should be from 1-45 can show me how and postive