import java.util.Random;
import javax.swing.JOptionPane;
public class randomNODUP {
public static void main(String[] args) {
String output = "";
int[] array1 = new int[6];
// copy random numbers into array1
for (int loop = 0; loop < 6; loop++) {
array1[loop] = rand1to49();
for (int loop2=0;loop2<6;loop++){
if (array1[loop]==array1[loop2]){
loop--;
}
}output = output + array1[loop];
}
JOptionPane.showMessageDialog(null, output + "\n", "Lottery",
JOptionPane.INFORMATION_MESSAGE);
}// end main
public static int rand1to49() {
int num;
// create a random number generator
Random numGenerator = new Random();
// generate a random number between 1 and 49 inclusive
num = Math.abs(numGenerator.nextInt(48)) + 1;
return num;
}// end random
}
I want 6 random numbers from 1 to 49, with no duplicates and then print them to the screen and store them to a 2d array(which might have values in already). For some reason it is not working! I know there are more advanced and easier ways to do this so any reply please stay on my beginner level.

New Topic/Question
Reply




MultiQuote






|