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:54 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
Rhino - 19 Dec 2005 00:01 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
[quoted text clipped - 6 lines]
>
> Alex Chien

It violates Usenet etiquette to multipost questions like this. I just spent
a fair while answering this one on comp.lang.java.help and I wouldn't have
put so much time into my answer if I had realized you already had answers on
another newsgroup. That just wastes my time, which is why it is rude for you
to multipost.

Rhino
Luc The Perverse - 19 Dec 2005 04:47 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
[quoted text clipped - 12 lines]
> already had answers on another newsgroup. That just wastes my time, which
> is why it is rude for you to multipost.

Likely he assumes if he posts his assignment in enough places someone will
do it for him

--
LTP

:)
oulan bator - 19 Dec 2005 08:50 GMT
here is the article I've promised :
Vitter, J.S. An efficient algorithm for sequential random sampling ACM
Trans. Math. Softw., ACM Press, 1987, 13, 58-67
Luc The Perverse - 19 Dec 2005 00:09 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
[quoted text clipped - 4 lines]
>
> anyone can help me how to write these two rules? THANKS.

What do you want us to do?   Do you not understand the assignment?   We're
not going to do it for you.

Here is an idea.

Make a list (array) of all the prime numbers 1 to 100, and then generate
only prime numbers for the first 10 attempts.    After ever item is added
compare it against a list of every number already made to make sure there
are no duplicates.

This is the shitty way to do it.

Better way is to make an array of every number 1 to 100 and then remove
items from the array as they are used.   Then you have no inefficient
checking.

Fill in the first 20 and keep track of the number of primes.  If you don't
have enough primes by the time you are out of spaces, fill the rest with
prime numbers only

--
LTP

:)
oulan bator - 19 Dec 2005 08:45 GMT
>Fill in the first 20 and keep track of the number of primes.  If you don't
>have enough primes by the time you are out of spaces, fill the rest with
>prime numbers only

warning Luc, this introduce a bias in the number of prime generated ...

>This is the shitty way to do it.
this is not that shitty if you look at the ratio he has 30 among 100,
"most" of the time nextInt() will return an unused integer, (I ve tried
it, it still working with 50 among 100

regards
Luc The Perverse - 19 Dec 2005 22:55 GMT
> >Fill in the first 20 and keep track of the number of primes.  If you
> >don't
>>have enough primes by the time you are out of spaces, fill the rest with
>>prime numbers only
>
> warning Luc, this introduce a bias in the number of prime generated ...

Forcing primes into a "random" sequence produces biases.

I VERY strongly feel that this is an assignment, which is designed to make
the student think of a way to implement this; something that this student
obviously does not want to do.

--
LTP

:)
Lasse Reichstein Nielsen - 19 Dec 2005 13:46 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.

I doubt those two rules are sufficient. You must also specify what
you mean by "random".

Should all primes occour with equal probability?
Should all non-primes occour with equal probability?
How should the number of primes be distributed? I any number of primes
between 10 and 25 equally likely, or are smaller numbers more likely
than larger?
How should the numbers be ordered?

Without knowing these constraints, a simple solution would be:
pick 10 primes at random
pick 20 nonprimes at random
(and "at random" might be not-very-random too)

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Roedy Green - 19 Dec 2005 22:22 GMT
>I doubt those two rules are sufficient. You must also specify what
>you mean by "random".

I interpreted the question to mean, integers are to be selected with a
pseudorandom number generator with uniform distribution in the range 1
.. 100 without regard to primality. However, a solution with less than
10 primes is not acceptable as is a solution with any number repeated.

Obviously that is not how you rapidly compute a solution, but I think
that is one way of describing the solution.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Googmeister - 19 Dec 2005 23:48 GMT
> >I doubt those two rules are sufficient. You must also specify what
> >you mean by "random".
[quoted text clipped - 6 lines]
> Obviously that is not how you rapidly compute a solution, but I think
> that is one way of describing the solution.

Yes, barring any additional info, this seems to be the natural
way to interpret the question. For the specific problem mentioned,
this is likely fast enough. To speed things up, you could select
30 of the 100 integers (using the first 30 iterations of the
standard shuffling algorithm), check if you have >= 10 primes.
If not, repeat.

Here's a somewhat more efficient method if the problem
size increase. Break this problem into two pieces: (i) determine
exactly how many primes to include, then (ii) randomnly choose
the required number of primes and nonprimes as above. To do
part (i), repeatedly generate Binomial(30, p) random variable
until you get a number >= 10. Here p  = fraction of primes in
desired range. This has the desired distribution, and it should
be reasonably efficient, even if the problem size increases.


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.