The Random Class The Random class is part of the java.util package

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Building Java Programs Chapter 5
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Computer Programming Lab(5).
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.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
Some Uses of Probability Randomized algorithms –for CS in general –for games and robotics in particular Testing Simulation Solving probabilistic problems.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
Mixing integer and floating point numbers in an arithmetic operation.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 posted – Due Feb 2PM Assignment 2 Posted – Due Feb 2 PM Finish Data and Expressions.
Floating Point ValuestMyn1 Floating Point Values Internally, floating point numbers have three pairs: a sign (positive or negative), a mantissa (has a.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 CMPT 126 Java Basics Using Classes and Objects.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Formatting Output & Enumerated Types & Wrapper Classes
Project 1.
Building Java Programs
Building Java Programs Chapter 5
using System; namespace Demo01 { class Program
© 2015 Pearson Education, Inc.
Chapter 3 Using Classes and Objects
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
© 2015 Pearson Education, Inc.
Using Classes and Objects (Chapter 3)
Chapter 3: Using Classes and Objects
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
The Random Class and its Methods
Recursive Definitions
Escape Sequences What if we wanted to print the quote character?
Chapter 6 More Conditionals and Loops
The ArrayList Class An ArrayList object stores a list of objects, and is often processed using a loop The ArrayList class is part of the java.util package.
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Chapter 3, cont Sept 20, 2004.
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Java so far Week 7.
Outline Boolean Expressions The if Statement Comparing Data
Chapter 3 Using Classes and Objects
Chapter 3 Using Classes and Objects
class PrintOnetoTen { public static void main(String args[]) {
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Outline Creating Objects The String Class The Random and Math Classes
Outline Boolean Expressions The if Statement Comparing Data
What to expect this week
Chapter 3 Using Classes and Objects
Presentation transcript:

The Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers A Random object performs complicated calculations based on a seed value to produce a stream of seemingly random values See RandomNumbers.java Copyright © 2017 Pearson Education, Inc.

//******************************************************************** // RandomNumbers.java Author: Lewis/Loftus // // Demonstrates the creation of pseudo-random numbers using the // Random class. import java.util.Random; public class RandomNumbers { //----------------------------------------------------------------- // Generates random numbers in various ranges. public static void main(String[] args) Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println("From 0 to 9: " + num1); continued Copyright © 2017 Pearson Education, Inc.

num1 = generator.nextInt(10) + 1; continued num1 = generator.nextInt(10) + 1; System.out.println("From 1 to 10: " + num1); num1 = generator.nextInt(15) + 20; System.out.println("From 20 to 34: " + num1); num1 = generator.nextInt(20) - 10; System.out.println("From -10 to 9: " + num1); num2 = generator.nextFloat(); System.out.println("A random float (between 0-1): " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println("From 1 to 6: " + num1); } Copyright © 2017 Pearson Education, Inc.

Sample Run A random integer: 672981683 From 0 to 9: 0 From 1 to 10: 3 A random float (between 0-1): 0.18538326 From 1 to 6: 3 continued num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1); num1 = generator.nextInt(15) + 20; System.out.println ("From 20 to 34: " + num1); num1 = generator.nextInt(20) - 10; System.out.println ("From -10 to 9: " + num1); num2 = generator.nextFloat(); System.out.println("A random float (between 0-1): " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println("From 1 to 6: " + num1); } Copyright © 2017 Pearson Education, Inc.

Quick Check Given a Random object named gen, what range of values are produced by the following expressions? gen.nextInt(25) gen.nextInt(6) + 1 gen.nextInt(100) + 10 gen.nextInt(50) + 100 gen.nextInt(10) – 5 gen.nextInt(22) + 12 Copyright © 2017 Pearson Education, Inc.

Quick Check Given a Random object named gen, what range of values are produced by the following expressions? Range 0 to 24 1 to 6 10 to 109 100 to 149 -5 to 4 12 to 33 gen.nextInt(25) gen.nextInt(6) + 1 gen.nextInt(100) + 10 gen.nextInt(50) + 100 gen.nextInt(10) – 5 gen.nextInt(22) + 12 Copyright © 2017 Pearson Education, Inc.

Quick Check Write an expression that produces a random integer in the following ranges: Range 0 to 12 1 to 20 15 to 20 -10 to 0 Copyright © 2017 Pearson Education, Inc.

Quick Check Write an expression that produces a random integer in the following ranges: Range 0 to 12 1 to 20 15 to 20 -10 to 0 gen.nextInt(13) gen.nextInt(20) + 1 gen.nextInt(6) + 15 gen.nextInt(11) – 10 Copyright © 2017 Pearson Education, Inc.