Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34925
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Will Math.random return 0.0? |
| Date | 2016-07-16 16:51 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nme6o3$1mro$1@gioia.aioe.org> (permalink) |
| References | <rand-20160716162126@ram.dialup.fu-berlin.de> <nmdusk$1bu7$1@gioia.aioe.org> |
On 7/16/2016 2:37 PM, Arne Vajhøj wrote: > On 7/16/2016 11:24 AM, Stefan Ram wrote: > The seed algorithm is: > > seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) > > and given that: > > mod(0x5DEECE66D,4) is 1 > 0xB is odd > & ((1L << 48) - 1) must be equivalent to mod pow(2,48) > > then the algorithm should have full cycle aka seed > will have all values including zero. > > so if nextDouble() just returned seed/pow(2,48) then > it will return 0.0 occasionally. > > But it does not. Instead it uses: > > return (((long)next(26) << 27) + next(27)) / (double)(1L << 53); > > So it will only return 0.0 if the high 26 bits of seed > and after update the high 27 bits of seed are both zero. > > Now my math skills are not good enough to evaluate > whether that is indeed the case or not. > > The above has pow(2,53) potential values but given the > seed cycle of pow(2,48) and the need for 2 calls, then > it will only be able to return pow(2,47) different values. Hmm. Eric's post just got me thinking. A given seed will only give pow(2,47) doubles in a cycle. But there must be two possible double cycles meaning that the answer (0.0 possible) may depend on the initial seed. That may complicate the test a bit. Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: Will Math.random return 0.0? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-16 14:37 -0400 Re: Will Math.random return 0.0? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-16 16:51 -0400
csiph-web