Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Random number generator

843789Nov 24 2009 — edited Nov 25 2009
Hi I am making a program that generates 100 Random numbers between 0-999 also it heads all the numbers below 100 with 0's eg 099 and 003. I also need to make sure the program generates all unique numbers(no numbers can be the same , if a number generated is the same it needs to create a different number until its different. ive generated the number creater and got all numbers to be headed with 0's but im really stuck on the generating all unique numbers part. Can anyone help please? heres my code :
import java.util.Random;
public class Random100 
{
	public static void main(String args[])
	
	{
		int x; 
		int [] Arrayofrandom = new int [100];
		Random RandomGenerated = new Random();
		
		for (x = 0; x<Arrayofrandom.length;x++)
		{	
			Arrayofrandom[x] = RandomGenerated.nextInt(999);
			
		}
		
		for (x=0; x<100;x++)
		{			
			if(Arrayofrandom[x]<10)
				System.out.print("00"+Arrayofrandom[x]+"\t");
			else if(Arrayofrandom[x]<100&&Arrayofrandom[x]>9)
				System.out.print("0"+Arrayofrandom[x]+"\t");
			else
				System.out.print(Arrayofrandom[x]+"\t");
		}
	
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2009
Added on Nov 24 2009
27 comments
677 views