Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

JAVA random number question

Thread view: 
hcchien420@gmail.com - 18 Dec 2005 22:55 GMT
I have to write a JAVA code to generate 30 random number from 1 to 100
in a 5 x 6 board of cells.. but I don't know how to write two rules
(1)The first rule is each number must be different in the 30 random
number.
(2)The second rule is in the 30 numbers, there should be at least 10
prime number in these 30 numbers.

anyone can help me how to write these two rules? THANKS.

Alex Chien
oulan bator - 18 Dec 2005 23:25 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

#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));
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.