Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Help!!! Generating Random Numbers
I keep getting duplicates, any suggestions??
public class exam
public static void main(Stringargs)
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] + "