bonus puzzle #1 random doubles [0,5] instructor: scott kristjanson cmpt 125/125 sfu burnaby, fall...
TRANSCRIPT
Bonus Puzzle #1Random Doubles [0,5]
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
2
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 2
Bonus Puzzle #1
Write an expression to calculate double random numbers from [0, 5.000000000] inclusive
Note: Generating the range: [0, 5.0) is easy: generator.nextFloat()*5
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
3
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 3
Honourable Mentions
Some good tries by:Amritpaul Gill
Gavin Haynes
Adam Tuck
A working solution by Raymond Zeng• But slow convergence, not guaranteed to ever complete
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
4
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 4
And the Winner is….
Serena Read:• Simple elegant well commented solution• Returns entire range with [0,5] inclusive• Fast convergence, but still may never terminate
public static float ranNum(){
Random ran = new Random();int a;float f, r;
do{a = ran.nextInt(6);f = ran.nextFloat();r = (float)a - f;
}while(r<0);
return r;}
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
5
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 5
Scott’s solution
Can we improve on Serena’s and Raymond’s solution?
• Want guaranteed convergence• Want to eliminate loops• Want to cover the whole range uniformly
(this last bullet is much trickier than you think!)
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
6
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 6
BUT DO WE WANT TO SOLVE THIS?
What is the practical difference between [0,5) and [0,5] ?• Remember when you compare floats, we useTolerances
• That means 4.999999 equals 5.000000 within a tolerance• How many real numbers are in between 4.999999 and 5.000000?
∞• How many java floating point numbers are in this gap?
ONE : 4.9999995
That is because floats only have 32 bits to represent the number
There is limited precision, and thus limited resolution.
Do we WANT to put all this effort into something whose probability of occurring approaches zero in the limit?
It took over 2 million tries for Serena’s algorithm to return 5.000000
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
7
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 7
Representing Real numbers in Java [2]
• Java uses the IEEE 754 standard:This uses 1-plus form of the binary normalized fraction (rounded).
• The fraction part is called the mantissa. • 1-plus normalized scientific notation base two is then:
N = ± (1.b1b2b3b4 ...)2 x 2+E
For a great overview of this standard, see [2] at:http://people.uncw.edu/tompkinsj/133/Numbers/Reals.htm
float
double
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
8
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 8
Ok, so you really want to solve this…
Only missing a single finite range with [0,5)
If we know the size of this gap,• We can add it in with 50/50 probability to close the gap uniformly
generator.nextFloat()*5 + ((generator.nextInt() >= 0) ? epsilon : 0);
What’s epsilon?It is based on the resolution (# bits in real number) plus the exponent (smaller magnitude numbers have finer resolution).
epsilon = (.5)23 = 0.00000000000000000000001B
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
9
Scott Kristjanson – CMPT 125/126 – SFU
But this assumes nextFloat works as expected…
Yes, nextFloat works, but is does not cover the entire [0,1) space uniformly.
nextFloat produces a random number based on 224 possible floats within this range (see [3] for details).
However there are really 230 possible floats in the range [0,1) using the IEEE 754 standard (see [2] for details).
That’s only about 1.6% of all possible floats that get returned.
If you really want to get them all, you will need to add in a 6 bit random number to the end of what nextFloat returns.
Instead of using a constant Epsilon as in previous slide, it needs to become a 6-bit random variable.
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
10
Scott Kristjanson – CMPT 125/126 – SFU
Wk04.1 Slide 10
References:
1. J. Lewis, P. DePasquale, and J. Chase., Java Foundations: Introduction to Program Design & Data Structures. Addison-Wesley, Boston, Massachusetts, 3rd edition, 2014, ISBN 978-0-13-337046-1
2. Tompkins, J.A. , Java Primitive Data Types - Reals - IEEE754 http://people.uncw.edu/tompkinsj/133/Numbers/Reals.htm
3. Oracle Reference Pages on the Random Class
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextFloat%28%29