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


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

Re: Will Math.random return 0.0?

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Will Math.random return 0.0?
Date 2016-07-16 19:39 +0000
Organization A noiseless patient Spider
Message-ID <nme2i6$81t$1@dont-email.me> (permalink)
References <rand-20160716162126@ram.dialup.fu-berlin.de>

Show all headers | View raw


On Sat, 16 Jul 2016 15:24:57 +0000, Stefan Ram wrote:

> 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?
> 
>   (When the documentation says that »random« might return 0.0, this
>   actually does not say that it ever will do so.)
>
Probably, but it may take some time.

I wrote an instrumented version of your program that accepts the number 
of random values to be generated and (for an eyeball check on Math.random) 
allows you to set the number of buckets that it uses to capture the 
generated random numbers, i.e, 20 buckets splits the range into groups 
that each captures 5% of the range from 0.0 to 1.0. For good measure it 
also captures the smallest and largest random numbers generated.

As the number of randoms generated increases, the min and max values get 
closer to 0.0 and 1.0 and the number of samples in each bucket become 
more even, which is what you'd expect, but even a run generating 10^9 (1 
billion) samples has limits of:
 Min value:     8.878034973847093E-10
 Max value:     0.9999999976070657

By comparison, the limits reached from a 1000 sample run were:
Min value:     7.924107404522207E-5
Max value:     0.9993267012123804

Meanwhile, your original code had been running for a while before I 
developed and played with my extended version and has currently used 
100.5 minutes of MPU time, which is close to 1 hour 40 since its using 
between 99 and 100% of the available CPU time from one of the two cores. 
This is running on a laptop powered by a 1.6 GHz Core Duo with 3GB of RAM 
and running Fedora 23 Linux.

My billion sample run took 132 seconds to run, or 7.6 million samples 
were generated and analysed per second.

I'll leave your code running to see if it stops any time thus evening.  


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

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


Thread

Re: Will Math.random return 0.0? Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-16 19:39 +0000
  Re: Will Math.random return 0.0? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-16 16:12 -0400
    Re: Will Math.random return 0.0? Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-16 20:32 +0000
      Re: Will Math.random return 0.0? Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-17 08:00 +0000
    Re: Will Math.random return 0.0? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-16 16:47 -0400
    Re: Will Math.random return 0.0? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-07-17 01:03 +0200

csiph-web