Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Bad documentation on java.util.Random#nextInt(int)

Reply
Thread Tools

Bad documentation on java.util.Random#nextInt(int)

 
 
tyrandor3@gmail.com
Guest
Posts: n/a
 
      01-03-2008
reference
http://java.sun.com/javase/6/docs/ap...ml#nextInt(int)

do {
bits = next(31);
val = bits % n;
} while (bits - val + (n-1) < 0);


big problem

bits>=0
n>0
n<=bits
therefore
0<=bits%n<bits
therefore bits-bits%n > 0
therefore bits-bits%n+n-1 > 0
therefore bits-bits+n-1<0 is always false?

So how can this loop ever iterate more than once as the documentation
says it can.
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-03-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> reference
> http://java.sun.com/javase/6/docs/ap...ml#nextInt(int)
>
> do {
> bits = next(31);
> val = bits % n;
> } while (bits - val + (n-1) < 0);
>
>
> big problem
>
> bits>=0
> n>0
> n<=bits
> therefore
> 0<=bits%n<bits
> therefore bits-bits%n > 0
> therefore bits-bits%n+n-1 > 0
> therefore bits-bits+n-1<0 is always false?
>
> So how can this loop ever iterate more than once as the documentation
> says it can.


Java int is 2's complement signed. If (n-1) and (bits-val) are both
large enough, their sum may wrap around to negative. Think of the "< 0"
test in the Java code as meaning, in mathematical integer arithmetic, ">
Integer.MAX_VALUE".

Patricia
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-04-2008
On Wed, 2 Jan 2008 21:16:00 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>So how can this loop ever iterate more than once as the documentation


next and nextInt are quite different animals.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
integer >= 1 == True and integer.0 == False is bad, bad, bad!!! rantingrick Python 44 07-13-2010 06:33 PM
Bad media, bad files or bad Nero? John Computer Information 23 01-08-2008 09:17 PM
ActiveX apologetic Larry Seltzer... "Sun paid for malicious ActiveX code, and Firefox is bad, bad bad baad. please use ActiveX, it's secure and nice!" (ok, the last part is irony on my part) fernando.cassia@gmail.com Java 0 04-16-2005 10:05 PM
24 Season 3 Bad Bad Bad (Spoiler) nospam@nospam.com DVD Video 12 02-23-2005 03:28 AM
24 Season 3 Bad Bad Bad (Spoiler) nospam@nospam.com DVD Video 0 02-19-2005 01:10 AM



Advertisments