Help!!! Generating Random Numbers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help!!! Generating Random Numbers

I keep getting duplicates, any suggestions??


import javax.swing.JOptionPane;
public class exam
{
public static void main(String[]args)
{
int maxn = 45 , index , dupindex,anum;
int numsneeded ,pass,comparison,temp;
String results ;
numsneeded = Integer.parseInt(JOptionPane.showInputDialog(null,"please enter number of lotto numbers :"));
int [] numbers = new int[numsneeded];
for ( index= 0; index < numsneeded;index ++)
{
anum = (int)(Math.random()* maxn)+1;
numbers[index] = anum;
for (dupindex = 0 ; numbers[dupindex] != anum;dupindex++)
if (index == dupindex)
index ++ ;
}
for (pass = 1 ; pass <= numbers.length - 1 ;pass++)
{
for (comparison = 1 ;comparison <= numbers.length - pass ; comparison ++)

{
if (numbers[comparison-1] >numbers[comparison])
{
temp = numbers[comparison - 1 ];
numbers[comparison - 1 ]= numbers[comparison];
numbers[comparison] = temp;
}
}
}
results = "your lotto numbers are " + "
";
for (index = 0 ; index< numbers.length;index ++)
results += numbers[index] + "
";
JOptionPane.showMessageDialog(null,results);
}
}
Sign In or Register to comment.