Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34914

Re: Will Math.random return 0.0?

From Thomas Richter <thor@math.tu-berlin.de>
Newsgroups comp.lang.java.programmer
Subject Re: Will Math.random return 0.0?
Date 2016-07-16 18:49 +0200
Organization University of Stuttgart, FRG
Message-ID <nmdoj7$o5v$1@news2.informatik.uni-stuttgart.de> (permalink)
References <rand-20160716162126@ram.dialup.fu-berlin.de>

Show all headers | View raw


Am 16.07.2016 um 17:24 schrieb Stefan Ram:
>   I wrote the following program.
> 
> public final class Main
> { public static void main( final java.lang.String[] args )
>   { while( java.lang.Math.random() != 0.0 );
>     java.lang.System.out.println( "0.0" ); }}
> 
>   What do you expect it's (average) runtime to be
>   on average hardware?
> 
>   Can we be sure that it will ever terminate?

No, you cannot. If random() is implemented by the average (=bad) linear
congruence generator, then zero will never be the output of random().
Whether it actually *can* be implemented by such a generator and still
satisfy the constraints of random() I cannot tell.

Otherwise, it depends on the period of the generator, of course, which
again is upper-bounded by the size of its internal state. So for
example, if random() has 48 state bits, the maximum possible period size
is 2^48 (which is a pretty huge number). This is because (despite its
name) random() is typically a determninistic algorithm, with 2^48
possible states (if it has 48 bit of internal storage), and hence, once
all 2^48 possible states have been visited, the generator needs to hit
one state it was already in before. Because it is deterministic, it will
from then on repeat the cycle again.

Greetings,
	Thomas

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Will Math.random return 0.0? Thomas Richter <thor@math.tu-berlin.de> - 2016-07-16 18:49 +0200
  Re: Will Math.random return 0.0? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-16 14:15 -0400
    Re: Will Math.random return 0.0? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-16 14:19 -0400

csiph-web