Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

class java.util.Random;

0 views
Skip to first unread message

sspurcell

unread,
May 1, 2001, 4:11:38 PM5/1/01
to
Hello,
I am trying to understand the class java.util.Random.
I am seeing it used in the following context.

static Random select = new Random();

// then later
int index = select.nextInt(5);

What is the nextInt doing? I am assuming that it selects a int
between 0 and 5? or am I mistaken.

Thanks
Scott

Roedy Green

unread,
May 1, 2001, 4:36:31 PM5/1/01
to
On Tue, 01 May 2001 13:11:38 -0700, sspurcell
<sspurcel...@sun.partner.remarq.com.invalid> wrote or quoted :

>I am trying to understand the class java.util.Random.
>I am seeing it used in the following context.

see "random number" in the Java glossary.


-
For more detail, please look up the key words mentioned in this post in
the Java Glossary at:
http://mindprod.com/gloss.html or http://209.153.246.39/gloss.html
If you don't see what you were looking for, complain!
or send your contribution for the glossary.
--
Roedy Green, Canadian Mind Products
Custom computer programming since 1963.
Almost ready to take on new work.

Michael Migal

unread,
May 1, 2001, 6:01:12 PM5/1/01
to
From the Java documentation:
public int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this random
number generator's sequence. The general contract of nextInt is that one int
value in the specified range is pseudorandomly generated and returned. All n
possible int values are produced with (approximately) equal probability. The
method nextInt(int n) is implemented by class Random as follows:
public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");

if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);

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

The hedge "approximately" is used in the foregoing description only
because the next method is only approximately an unbiased source of
independently chosen bits. If it were a perfect source of randomly chosen
bits, then the algorithm shown would choose int values from the stated range
with perfect uniformity.

The algorithm is slightly tricky. It rejects values that would result in
an uneven distribution (due to the fact that 2^31 is not divisible by n).
The probability of a value being rejected depends on n. The worst case is
n=2^30+1, for which the probability of a reject is 1/2, and the expected
number of iterations before the loop terminates is 2.

The algorithm treats the case where n is a power of two specially: it
returns the correct number of high-order bits from the underlying
pseudo-random number generator. In the absence of special treatment, the
correct number of low-order bits would be returned. Linear congruential
pseudo-random number generators such as the one implemented by this class
are known to have short periods in the sequence of values of their low-order
bits. Thus, this special case greatly increases the length of the sequence
of values returned by successive calls to this method if n is a small power
of two.


Parameters:
n - the bound on the random number to be returned. Must be positive.
Returns:
a pseudorandom, uniformly distributed int value between 0 (inclusive)
and n (exclusive).
Throws:
IllegalArgumentException - n is not positive.

"sspurcell" <sspurcel...@sun.partner.remarq.com.invalid> wrote in
message news:00000019...@usw-ex0108-192.remarq.com...

Patricia Shanahan

unread,
May 1, 2001, 9:00:46 PM5/1/01
to

To find out what a method in the standard class libraries does, go
directly to the API documentation, which you can download or browse
on-line at e.g. http://java.sun.com/j2se/1.3/docs/api/index.html.

Patricia

Tim Tyler

unread,
May 11, 2001, 9:36:46 AM5/11/01
to
sspurcell <sspurcel...@sun.partner.remarq.com.invalid> wrote:

: int index = select.nextInt(5);

: What is the nextInt doing? I am assuming that it selects a int
: between 0 and 5? or am I mistaken.

It returns either 0, 1, 2, 3 or 4, at pseudo-random.
--
__________ http://rockz.co.uk/ http://alife.co.uk/ http://hex.org.uk/
|im |yler http://atoms.org.uk/ http://mandala.co.uk/ t...@iname.com

EsoralTrebor

unread,
May 11, 2001, 1:22:53 PM5/11/01
to

EsoralTrebor

unread,
May 11, 2001, 3:04:04 PM5/11/01
to
I don't know why my original response was not added to my
message.

The Random.nextInt() takes an int value and uses it as bits so
if you put 5 it will generate a number using 5 bits (0 to 31).

I hope this helps.

Robert

Roedy Green

unread,
May 11, 2001, 3:38:29 PM5/11/01
to
On Fri, 11 May 2001 13:36:46 GMT, Tim Tyler <t...@cryogen.com> wrote or
quoted :

>
>: What is the nextInt doing? I am assuming that it selects a int
>: between 0 and 5? or am I mistaken.

That is one of the gotchas. There are others. See "pseudorandom
numbers" in the Java glossary for details.

Roedy Green

unread,
May 11, 2001, 5:14:53 PM5/11/01
to
On Fri, 11 May 2001 12:04:04 -0700, EsoralTrebor
<%45soral%54r...@sun.partner.remarq.com> wrote or quoted :

>The Random.nextInt() takes an int value and uses it as bits so
>if you put 5 it will generate a number using 5 bits (0 to 31).

nope. You have it confused with the other methods.

See "pseudorandom numbers" in the Java glossary.

Tim Tyler

unread,
May 12, 2001, 3:21:43 AM5/12/01
to
EsoralTrebor <%45soral%54r...@sun.partner.remarq.com> wrote:

: The Random.nextInt() takes an int value and uses it as bits so


: if you put 5 it will generate a number using 5 bits (0 to 31).

: I hope this helps.

That's unlikely :-|

That's the (non-public) next(int) method - not the nextInt(int) method.

0 new messages