Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.

Slides:



Advertisements
Similar presentations
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
Advertisements

Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Random Numbers Dick Steflik. Pseudo Random Numbers In most cases we do not want truly random numbers –most applications need the idea of repeatability.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
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.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Compunet Corporation Programming with Visual Basic.NET Random Number Week # 12 Tariq Ibn Aziz & Kevin Jones.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
©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.
Creating Effective Power Point Presentations Presented by: Amanda Louie CSC 105 LF08 December 2, 2003.
4 6 Base Exponent The exponent is sometimes referred to as the power.
Rules of Integers. Positive numbers are numbers that are above zero. Negative numbers are numbers below zero.
Psy B07 Chapter 1Slide 1 ANALYSIS OF VARIANCE. Psy B07 Chapter 1Slide 2 t-test refresher  In chapter 7 we talked about analyses that could be conducted.
Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Chapter 4: Loops and Files
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Experimental Method and Data Process: “Monte Carlo Method” Presentation # 1 Nafisa Tasneem CHEP,KNU
CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.
© 2006 Pearson Education Making Decisions 1 of 26 MAKING DECISIONS Booleans Basic Choices (Should I stay or should I go?) Choosing From Many Alternatives.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming Math Random Dice.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
Review for Nested loops & Math class methods & User defined methods.
Random Numbers. Are a series of numbers that have no pattern to them Ex) 7, 31, 4, 9, 8, 99… Random Numbers are used in… - Computer Games - Lotteries(6-49)
Random Numbers RANDOM VS PSEUDO RANDOM. Truly Random numbers  From Wolfram: “A random number is a number chosen as if by chance from some specified distribution.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating and Using Class Methods. Definition Class Object.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Lesson 4.5 Concept: How to Multiply Integers Guidelines: When multiplying integers….. If the integer signs are the same, the product is positive ( + +)
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.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
FACTORING – Common Terms Factoring an algebraic expression helps us to simplify the expression. It puts the expression into easier terms for use when dealing.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
Module 9.2 Simulations. Computer simulation Having computer program imitate reality, in order to study situations and make decisions Applications?
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
AP Java 10/4/2016.
JavaScript Functions.
Random numbers Taken from notes by Dr. Neil Moore
The Random Class and its Methods
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Random vs pseudo random
Variables and Data Types
Random number generators
Random Numbers while loop
Presentation transcript:

Let’s not leave anything to chance

How that process to generate random numbers takes places requires some complicated statistics that are outside the scope of this course. But there are a couple of things to keep in mind: 1. The process focuses on creating a even distribution of randomly chosen numbers. 2. The computer pulls a seed from somewhere to help to randomize the process. It is sometimes the previous random number chosen or the system time. 3. No number generated is ever truly random but they come close enough.

The most common way to obtain random numbers in JAVA is to use the random method of the Math class. This method returns a number between zero and up to but no including 1. So the method could return a.123, 0,.89,.5, but it would never return a 1.

You can change the range of numbers for Math.random() by using a multiplier and an offset in the following way: multiplier*Math.random() + offset So if you wanted to generate numbers between 300 and 400, you would use the following: 101 * Math.random() Note that the multiplier is 101 because it is 101 numbers between 300 and 400 inclusively

Math.random() returns a double. To obtain an integer, you can just cast the output as an integer in the following manner: (int)(101 * Math.random() + 300) Do NOT do the following. It will result in a zero since it casts the output of Math.random() as an int which always produces a 0. Always cast after multiplying by the multiplier. 100 * (int)Math.random()

JAVA also provides a Random Class which provides a larger variety of options. Look at the API and Lesson 30 in Blue Pelican for more details. It provides both a nextDouble() method which works the same way as Math.random() as well as nextInt(n) which returns a random int from 0 up to n-1