or you can use a Container in which duplicate values aren't possible.
Such as Set.
Initializing the set (of the Integer type)
QUOTE
Set<Integer> testing = new TreeSet<Integer>();
then after generating your random number (the variable is named 'temp' in this example)
you check (with the contains-method), if the value is already in your Set and if it is, simply generate a new one:
CODE
Integer temp = Integer.valueOf(new Double(BASE * Math.random() + 1).intValue());
while(testing.contains(temp))
{
temp = Integer.valueOf(new Double(BASE * Math.random() +1).intValue());
}
testing.add(temp);
whereas the output is a bit different:
CODE
++loopCount;
Iterator<Integer> iter = testing.iterator();
while(iter.hasNext())
{
System.out.printf("%3d, ", (Integer)iter.next());
}
testing.clear();//to empty the set
The advantage of using the Set is also that you get an sorted output:
CODE
3, 6, 14, 21, 25, 26,
1, 2, 10, 17, 20, 26,
23, 27, 30, 32, 36, 38,
1, 3, 27, 29, 40, 49,
3, 10, 22, 28, 35, 49,
I hope I could help you a bit.
EDIT: spellckeck
This post has been edited by goldenthunder: 18 Sep, 2007 - 07:43 AM