Re: [scala-functional] EXERCISE 6.2: generating a random Double from a random Int

2018-04-10 Thread Ben Hutchison
On Tue, Apr 10, 2018 at 8:24 AM, Adam Mackler  wrote:
>
> Maybe I'm missing something, but it seems to me that since a Double is
> represented using 64 bits and an Int 32, that using the amount of
> randomness in an Int to generate a random Double will mean that the results
> will be unevenly distributed, perhaps with possible values of the Double
> never returned.
>


I agree with your analysis. Generating Doubles from a single Int is no a
good real world strategy.  I guess it was just posed as an exercise.

-Ben

> --
> You received this message because you are subscribed to the Google Groups
> "scala-functional" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scala-functional+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[scala-functional] EXERCISE 6.2: generating a random Double from a random Int

2018-04-10 Thread Adam Mackler
Exercise 6.2 on page 83:

*Write a function to generate a Double between 0 and 1 , not including 1.*


The given hint and answer indicate generating a non-negative Int and then 
dividing it by the one more than the maximum possible Int value.

Maybe I'm missing something, but it seems to me that since a Double is 
represented using 64 bits and an Int 32, that using the amount of 
randomness in an Int to generate a random Double will mean that the results 
will be unevenly distributed, perhaps with possible values of the Double 
never returned.  Here's what I came up with:

  def double(rng: RNG): (Double, RNG) = {
val (int1, rng2) = rng.nextInt
val (int2, rng3) = rng2.nextInt
val long = (int1.toLong << 32) | int2// this is a random 
Long
val nnLong = if (long < 0) -(long + 1) else long // non-negative random 
Long
val rLong = -(nnLong.toDouble / Long.MinValue)   // random double
(rLong, rng3)
  }


I generate two random integers, convert one to a Long and bit-shift it 32 
bits to the left and then OR it with the other Int.  This should give me a 
random Long.  Then I make it non-negative, divide it by the minimum Long 
value, and negate that.  Intuitively this seems to me that it would give a 
more even distribution since there are extra bits of randomness from the 
second Int.  Is my thinking correct?  Is this worse or better than the 
answer given in the book?

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.