Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.

Slides:



Advertisements
Similar presentations
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved Floating-point Numbers.
Advertisements

2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Chapter 2: Data Representation
Principles of Computer Architecture Miles Murdocca and Vincent Heuring Chapter 2: Data Representation.
ITEC 352 Lecture 7 Binary(2). Review Homework due on Friday Questions Binary Addition Subtraction Encoding.
Faculty of Computer Science © 2006 CMPUT 229 Floating Point Representation Operating with Real Numbers.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Floating Point Numbers
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Representing Real Numbers Using Floating Point Notation Lecture 6 CSCI 1405, CSCI 1301 Introduction to Computer Science Fall 2009.
CHAPTER 5: Floating Point Numbers
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM.
The Binary Number System
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
IEEE Floating Point Numbers Overview Noah Mendelsohn Tufts University Web: COMP.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Numbers and number systems
Information Representation (Level ISA3) Floating point numbers.
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Numeric precision in SAS. Two aspects of numeric data in SAS The first is how numeric data are stored (how a number is represented in the computer). –
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
IT253: Computer Organization
1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24.
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
CISE301_Topic11 CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4:
Computing Systems Basic arithmetic for computers.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles of Computer.
ECE232: Hardware Organization and Design
Analysis of Algorithms Chapter 11 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
Floating Point (a brief look) We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large numbers,
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSC 221 Computer Organization and Assembly Language
Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
EE459 I ntroduction to Artificial I ntelligence Genetic Algorithms Practical Issues: Representations.
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Floating Point Numbers
Introduction to Numerical Analysis I
Floating Point Representations
Dr. Clincy Professor of CS
Introduction To Computer Science
Computer Architecture & Operations I
Number Systems and Binary Arithmetic
Data Structures Mohammed Thajeel To the second year students
Luddy Harrison CS433G Spring 2007
CSCI206 - Computer Organization & Programming
Introduction to Java, and DrJava part 1
How to represent real numbers
How to represent real numbers
Dr. Clincy Professor of CS
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
Introduction to Primitives
Introduction to Java, and DrJava part 1
Presentation transcript:

Bonus Puzzle #1 Random 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, ] 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 equals within a tolerance How many real numbers are in between and ? ∞ How many java floating point numbers are in this gap? ONE : 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

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).normalized The fraction part is called the mantissa.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: 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 = B

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 2 24 possible floats within this range (see [3] for details). However there are really 2 30 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 Tompkins, J.A., Java Primitive Data Types - Reals - IEEE Oracle Reference Pages on the Random Class