Introduction to programming in java Lecture 16 Random.

Slides:



Advertisements
Similar presentations
The Random Class.
Advertisements

Computer Science 209 The Singleton Pattern. Random Numbers System.out.println((int)(Math.random() * 6) + 1); Math.random() uses a single generator that.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Building Java Programs
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
2-May-15 Randomized Algorithms. 2 Also known as Monte Carlo algorithms or stochastic methods A short list of categories Algorithm types we will consider.
Building Java Programs
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.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
©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 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,
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.
Random numbers in Python Nobody knows what’s next...
©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.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Section 5.1 What is Probability? 5.1 / 1. Probability Probability is a numerical measurement of likelihood of an event. The probability of any event is.
Objects and Classes Plan for Today - we will learn about: the difference between objects and primitive values how to create (or construct) objects some.
Modeling and Simulation
CS305j Introduction to Computing Odds and Ends 1 Topic 16 Creating Correct Programs "It is a profoundly erroneous truism, repeated by all the copybooks,
Chapter 2 Elementary Programming
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
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.*;
C++ Programming Lecture 10 Functions – Part II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
Using Java Class Library
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
CompSci Classwork today  Back to bouncing balls  Create a new BouncingSmiley using inheritence  Create an Arraylist of bouncing balls and smileys.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Random numbers in C++ Nobody knows what’s next....
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Probability Distributions and Expected Value
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers; Type boolean reading: , 5.6.
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.
Utility classes 1.Vector class 2.Random class 3.Date class 4.Scanner.
Random Numbers Eric Roberts CS 106A January 22, 2010.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
Monte Carlo Methods Some example applications in C++
Building Java Programs
Building Java Programs
Building Java Programs Chapter 5
Building Java Programs
Building Java Programs

The Random Class and its Methods
Random Another fun module.
CS005 Introduction to Programming
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.
Introduction to Classes and Methods
Randomized Algorithms
Building Java Programs
Random number generators
CS150 Introduction to Computer Science 1
Building Java Programs
Building Java Programs
Presentation transcript:

Introduction to programming in java Lecture 16 Random

Introduction Programs frequently need random numbers. – A card game uses random numbers to create a shuffled deck of cards. – A dice game uses random numbers to simulate a throw. – Many activities in the real world (such as traffic on a highway system) appear to be random, and a program that models them must use random numbers.

Random Number A single die can be regarded as a random number generator that selects a number from the range 1 through 6, and each number is equally likely. The same is true for a second throw of the die. The outcome of the first throw does not affect the outcome of the second throw (or of any other throw).

The Random class The Random class can be used in programs that need random numbers. Random is part of the java.util package.

The Random class (Cont…) A Random object is started out with a seed value which determines the sequence of numbers it will produce. Every Random object started with the same seed value will produce the same sequence of numbers. There are two constructors for Random objects:

The Random class (Cont…) Random() — Creates a new random number generator using a seed based on the current time. Random(long seed) — Creates a new random number generator based on a specific seed value.

The Random class (Cont…) Here is an example of constructing a random number generator: Random rand = new Random(); Now the methods of the Random class can be used with rand.

nextInt() Here are two methods (out of several) that Random objects provide. The second method is the one most often used.

Die Toss Program with out Seed

Die Toss Program with Seed

Floating Point Random Methods float nextFloat() — Returns a pseudorandom, floating point value in the range 0.0 up to but not including 1.0. double nextDouble() — Returns a pseudorandom, double-precision value in the range 0.0 up to but not including 1.0.

More Random Methods long nextLong() — Returns a pseudorandom, uniformly distributed long value. All possible long values, both positive and negative, are in the range of values returned. boolean nextBoolean() — Returns a pseudorandom boolean value.

Practice Question Write a program which generators password for the user. Password should be consist of small letters, capital letters and numbers.