#1: an "unefficient" way to do (as 30 elements is not much) y ou can
make a loop over the nextInt() until it gives a different number
#2 : you can make a nextPrime():int method that generates a prime in
0..100, using an isPrime() method and a loop over nextInt() ...
generate 10 primes first then and only then the 20 others numbers
(applying rule 1 only)
this is a way too handle your problem, it is not very scalable
I would first improve #2 building a static array of all primes ( lower
than or equal to 100). and generate an integer between 0..
array.length-1 (it would be much efficient)
if 100 remain low (by low I mean lower than usual number of int that
can be stored)
you can improve the algorithm by using a Set of primes, and a Set of
others values. Then you only have to generate indexes (first 10 in
primes set, remaining in the Set of other values)
otherwhise there are some algorithms (I can send you references)
Regards
Andrew McDonagh - 18 Dec 2005 23:40 GMT
> #1: an "unefficient" way to do (as 30 elements is not much) y ou can
> make a loop over the nextInt() until it gives a different number
[quoted text clipped - 18 lines]
>
> Regards
right about the inefficiency...ignoring the prime numbers part....
HashSet uniqueRandomNumbers = new HashSet(100);
java.util.Random generator = new Random();
for (int count = 0; uniqueRandomNumbers.size() < 100; count++) {
uniqueRandomNumbers.add(generator.nextInt(aUpperLimit));
}