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


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

Re: Will Math.random return 0.0?

Started byThomas Richter <thor@math.tu-berlin.de>
First post2016-07-16 18:49 +0200
Last post2016-07-16 14:19 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#34914 — Re: Will Math.random return 0.0?

FromThomas Richter <thor@math.tu-berlin.de>
Date2016-07-16 18:49 +0200
SubjectRe: Will Math.random return 0.0?
Message-ID<nmdoj7$o5v$1@news2.informatik.uni-stuttgart.de>
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

[toc] | [next] | [standalone]


#34917

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-07-16 14:15 -0400
Message-ID<nmdtk7$19to$1@gioia.aioe.org>
In reply to#34914
On 7/16/2016 12:49 PM, Thomas Richter wrote:
> 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.

Sure?

>             If random() is implemented by the average (=bad) linear
> congruence generator, then zero will never be the output of random().

Not true.

First LCG's can have seed zero and a good cycle as long as the add part 
is non-zero.

Second even for an MLCG (with add part zero) where seed can not be
zero, then the random numbers can include zero simply by subtracting
one.

Arne

[toc] | [prev] | [next] | [standalone]


#34919

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-07-16 14:19 -0400
Message-ID<nmdtr1$f2f$1@dont-email.me>
In reply to#34917
On 7/16/2016 2:15 PM, Arne Vajhøj wrote:
> On 7/16/2016 12:49 PM, Thomas Richter wrote:
>> 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.
>
> Sure?
>
>>             If random() is implemented by the average (=bad) linear
>> congruence generator, then zero will never be the output of random().
>
> Not true.
>
> First LCG's can have seed zero and a good cycle as long as the add part
> is non-zero.
>
> Second even for an MLCG (with add part zero) where seed can not be
> zero, then the random numbers can include zero simply by subtracting
> one.

     Third, no law requires that *all* the state bits become part
of the output.  (Indeed, for java.util.Random *no more than* 32 of
the 48 bits of state contribute to the output of next(), and in the
particular case of nextDouble() only 53 high-order bits from two
consecutive 48-bit states participate.)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web