The Random Class and its Methods

Slides:



Advertisements
Similar presentations
The Random Class.
Advertisements

Building Java Programs
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Describing Probability
Building Java Programs Chapter 5
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Confirmation Dialog Formatting Output.
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.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Bell Work: Factor x – 6x – Answer: (x – 8)(x + 2)
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
A multiple-choice test consists of 8 questions
Binomial & Geometric Random Variables §6-3. Goals: Binomial settings and binomial random variables Binomial probabilities Mean and standard deviation.
Copyright © 2015, 2011, 2008 Pearson Education, Inc. Chapter 7, Unit A, Slide 1 Probability: Living With The Odds 7.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
III. Probability B. Discrete Probability Distributions
Section 11.4 Tree Diagrams, Tables, and Sample Spaces Math in Our World.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
1.4 Equally Likely Outcomes. The outcomes of a sample space are called equally likely if all of them have the same chance of occurrence. It is very difficult.
Using Java Class Library
Simulating Probabilistic Behaviour
SECTION 11-2 Events Involving “Not” and “Or” Slide
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Probability and Simulation The Study of Randomness.
A die is rolled. What is the probability that the number showing is odd? Select the correct answer
Introduction to programming in java Lecture 16 Random.
Adding Probabilities 12-5
Monte Carlo Methods Some example applications in C++
Probability and Odds PSSA Unit.
What Is Probability?.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 5
Building Java Programs
Bring a penny to class tomorrow
© 2016 Pearson Education, Ltd. All rights reserved.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Building Java Programs
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.

Building Java Programs
PROBABILITY The probability of an event is a value that describes the chance or likelihood that the event will happen or that the event will end with.
Random Another fun module.
The probability of event P happening is 0. 34
Building Java Programs
Building Java Programs
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Probability.
PROBABILITY.
Multiply the probability of the events together.
Building Java Programs
Building Java Programs
Probability.
COUNTING AND PROBABILITY
PROBABILITY Lesson 10.3A.
Probability Simulations
Simulation Discrete Variables.
Warm Up Write each fraction as a percent % 37.5% 100%
Building Java Programs
Objectives Find the theoretical probability of an event.
“And” Probabilities.
Building Java Programs
Chapter 11 Probability.
Sets, Combinatorics, Probability, and Number Theory
Presentation transcript:

The Random Class and its Methods Random Numbers Using The Random Class November 12, 2018 The Random Class and its Methods

Random Numbers in Applications Some applications such as games and simulations require the use of randomly generated numbers. Tossing a coin: an even number may represent a head and an odd number may represent a tail A random number between one and six may represent the roll of one die A random number between 1 and 52 may represent a particular card in a deck of cards A roulette wheel has 38 slots numbered 0-36 and 00. One may use a random number between 0 and 37 to simulate a spin of the wheel. November 12, 2018 The Random Class and its Methods

The Random Class and its Methods The Java API has a class named Random for this purpose. To use the Random class, use the following import statement and create an instance of the class. import java.util.Random; Random randomNumber = new Random(); November 12, 2018 The Random Class and its Methods

Some Methods of the Random Class Description nextDouble() Returns the next random number as a double. The number will be within the range of 0.0 and 1.0. nextFloat() Returns the next random number as a float. The number will be within the range of 0.0 and 1.0. nextInt() Returns the next random number as an int. The number will be within the range of an int, which is –2,147,483,648 to +2,147,483,648. nextInt(int n) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n.

The Random Class and its Methods Using the Random Class The methods in the previous table generate uniformly distributed random numbers Each value in the range is equally likely to occur The type of data returned is determined by the name of the method (see previous slide – nextDouble, nextInt, …) November 12, 2018 The Random Class and its Methods

Range of Random Numbers The nextInt (n) method generates a random number in the range between 0 and n. 0 is included in the range but n is not included in the range nextInt (4) would generate 0, 1, 2, or 3 with equal probability. No other result is possible. To generate a random integer between min and max inclusively, use an expression such as the following where r is an object of Random: int num = r.nextInt (max – min + 1) + min; // random int between 11 and 15 inclusively int ans = r.nextInt (5) + 11; November 12, 2018 The Random Class and its Methods

The Random Class and its Methods Range of Doubles To generate a double between d1 and d2 use: double rand = r.nextDouble ( ) * (d2 – d1) + d1; To generate a random double between 800 and 1000 we would use double value = r.nextDouble( ) * (200.) + 800.; November 12, 2018 The Random Class and its Methods

Random int’s between 0 and 99 Sample output Random int’s between 0 and 99 November 12, 2018 The Random Class and its Methods