# Duke CPS 006 - Using Random Numbers (9 pages)

Previewing pages 1, 2, 3 of 9 page document
View Full Document

## Using Random Numbers

Previewing pages 1, 2, 3 of actual document.

View Full Document
View Full Document

## Using Random Numbers

273 views

Pages:
9
School:
Duke University
Course:
Cps 006 - Program Design and Methodology I
##### Program Design and Methodology I Documents
• 20 pages

• 4 pages

• 4 pages

• 20 pages

• 2 pages

• 9 pages

• 13 pages

• 24 pages

• 20 pages

• 11 pages

• 9 pages

• 51 pages

• 20 pages

• 5 pages

• 6 pages

• 24 pages

• 4 pages

• 28 pages

• 6 pages

• 3 pages

• 36 pages

• 3 pages

• 3 pages

• 11 pages

• 16 pages

• 13 pages

• 16 pages

• 6 pages

• 16 pages

• 36 pages

• 10 pages

• 19 pages

• 6 pages

• 8 pages

• 2 pages

• 6 pages

• 26 pages

• 6 pages

• 4 pages

• 36 pages

• 6 pages

• 20 pages

• 2 pages

• 2 pages

• 10 pages

• 5 pages

• 9 pages

• 41 pages

• 5 pages

Unformatted text preview:

Using Random Numbers Why Games Serious simulations Visual effects Obfuscation There are two standard ways to get random number Use random method of Math class Use Random class Both use same underlying arithmetic Pseudo random Numbers Using Math random Returns double in range 0 n 1 0 o Does not include 1 0 Simulate Die Roll int roll 1 int Math random 6 CompSci 6 8 1 Using Random Numbers Why use Random Class Provides many methods that are convenient to use Serious simulations Visual effects Obfuscation Twenty sided Die Random gen new Random int roll 1 gen nextInt 20 Some Other Methods Provided int nextDouble long nextLong int nextInt boolean nextBoolean void setSeed long n CompSci 6 Why 8 2 Using Random Numbers Think of ways to use random numbers in Image processing 1 2 3 4 5 6 7 CompSci 6 8 3 String Methods There are many useful methods The one used most frequently are summarized below Make sure you understand the notations and what is means int length boolean equals String s int compareTo String s 1 0 1 String substring int begin int end String substring int begin int indexOf String key 1 if not found char charAt int position Example What are the values String demo How are things demo substring 8 12 demo indexOf wa demo indexOf w a demo charAt 7 demo comparetTo how are things CompSci 6 8 4 Lab Today Animation Canvas CompSci 6 8 5 Canvas Details What are the dimensions of the Canvas java awt Dimension Creating the Canvas final java awt Dimension SIZE new java awt Dimension 600 600 Canvas display new Canvas SIZE Accessing the dimensions int height SIZE height int width SIZE width CompSci 6 8 6 Paint a circle Center of the screen Point x y java awt point Point center new Point Pen Set the color Java awt Color RED Fill the oval CompSci 6 8 7 Paint a Circle cont Canvas has a special method paintComponent must spell it exactly this way This method is automatically invoked when an event occurs o When main sets isVisible to true o When a window is resized CompSci 6 8 8 Lab

View Full Document

Unlocking...