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

Welcome to the new platform of Programmer's 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 its 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

Posts: 2Member
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.