Java Beginners

Moderators: zibadian
Number of threads: 1285
Number of posts: 2739

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help!!! Generating Random Numbers Posted by Buttons88 on 21 Nov 2012 at 2:44 PM
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 " + "\n";
for (index = 0 ; index< numbers.length;index ++)
results += numbers[index] + "\n";
JOptionPane.showMessageDialog(null,results);
}
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.