SMcGwier SMcGwier - 1 year ago 74
Java Question

Generating Random Numbers in For Loop from External Class

First of all, I apologize if this has been asked before and I couldn't find it. I scoured the depths of the internet looking for some guidance but did not come up successful.

In my programming II class we are working on creating a program for the lottery. We have created external classes (did that in the classroom) and have used the objects created in those classes in our driver. For this assignment, we are creating a program which outputs lottery numbers. The program asks the user if they are playing Pick 3, 4, or 5 and then is supposed to output a random number for each "ball". In one of the separate classes we have already imported and instantiated the randomizer. However, when I program the array and for loop, while it generates the proper number of printouts (3 printouts for 3, 4 printouts for 4, and 5 printouts for 5), a new number is not being generated for each loop. Instead, it is holding onto the first value and printing that out for each loop after. I've included my code below. Please help!

import java.util.Scanner;

public class LotteryGame2 {

public static void main(String[] args){

//Declare and instantiate objects
PickGame2 pick = new PickGame2();
Scanner keyboard=new Scanner(;

//Ask for and obtain user input
System.out.print("Are you playing Pick 3, Pick 4, or Pick 5? Enter number here: ");
int numberOfGame=keyboard.nextInt();
PickGame2[] gamenumber=new PickGame2 [numberOfGame];

for (int i=0; i<gamenumber.length; ++i){
int ball=pick.pullBall();
System.out.println("Ball " + (i+1) + " is " + ball);
}//Ending bracket of for loop

//Close Scanner object

}//Ending bracket of main method
}//Ending bracket of class LotteryGame2

The external classes are as follows:

private int ball;
private Random randomizer;

public LotteryContainer(){
this.randomizer=new Random();
}//Ending bracket of constructor

public void activate(){
this.ball=this.randomizer.nextInt(9) + 1;
}//Ending bracket of method activate

public int getBall(){
return this.ball;
}//Ending bracket of method getBall


private LotteryContainer machine;

public PickGame2(){
this.machine=new LotteryContainer();
}//Ending bracket of constructor

public void activate(){
}//Ending bracket of method activate

public int pullBall(){
return this.machine.getBall();
}//Ending bracket of pullBall

Answer Source

If you look at your PickGame2 class, you'll see that it uses a LotteryContainer for picking the balls. And the LotteryContainer has two methods - one that picks a number for the ball (activate) and one that returns the ball number that was picked (getBall). Since you only ever call pullBall in PickGame2, and pullBall only calls getBall from LotteryContainer, it means you never call the activate method which gets you a new ball.

Add a call to activate before every call to pullBall, and you'll get a different number printed each time.

(Actually, it's possible for a random number generator to generate the same number several times. I don't know what the specification of your homework was, but if it's supposed to simulate a "real" lottery where each ball is different, then just generating random numbers and displaying them is not enough).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download