title: How to convert unsigned long long to double
body: <p>I have a random number generator as follows.</p>
  
  <pre><code>uint64_t s[ 2 ];
  
  uint64_t next(void) { 
      uint64_t s1 = s[ 0 ];
      const uint64_t s0 = s[ 1 ];
      s[ 0 ] = s0;
      s1 ^= s1 &lt;&lt; 23; // a
      return ( s[ 1 ] = ( s1 ^ s0 ^ ( s1 &gt;&gt; 17 ) ^ ( s0 &gt;&gt; 26 ) ) ) + s0; // b, c
  }
  </code></pre>
  
  <p>I would like in fact to get doubles in the range (0,1).  One option is to do this in my code</p>
  
  <pre><code>#define M_RAN_INVM64 5.42101086242752217003726400434970855e-20
  [..]
  double u1;
  u1 = next()*M_RAN_INVM64;
  </code></pre>
  
  <ol>
  <li>I am worried I am losing precision and also that this is platform dependent.</li>
  <li>I don't want ever to get the values 0 or 1.</li>
  </ol>
  
  <hr>
  
  <p>One option is to cast next() to signed long long, multiply by M_RAN_INVM64 and then add 0.5.  I can then add 2^(64)/2 to make sure I never get 0 I think.</p>