lec16

lec16 - CSE 8A: Lecture 16 Random number generation...

This preview shows pages 1–6. Sign up to view the full content.

Page 1 of 21 CSE 8A, UCSD LEC 16 CSE 8A: Lecture 16 Random number generation Randomizing a sequence Automatic documentation generation and the Javadoc utility Package naming, and CLASSPATH (Reading: Savitch Ch 5 on Javadoc and Packages)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 2 of 21 CSE 8A, UCSD LEC 16 Random number generation Random numbers are useful in many applications: cryptography, software testing, games, probabilistic data structures, etc. A truly random number would be truly unpredictable However, algorithms are predictable! So, you don’t write a program to generate truly random numbers on a computer; you generate pseudo random numbers (you could connect your computer to a nonalgorithmic device and collect truly random bits from it; see /dev/random on Unix systems, or various web services) A good pseudorandom number generator will generate a sequence of numbers that will pass most statistical tests of randomness. .. the numbers will look very random How to do that?
Page 3 of 21 CSE 8A, UCSD LEC 16 Linear congruential generators A typical pseudorandom number generator works like this: The generator is initialized by setting the value of a “seed” (The sequence of numbers generated will depend on the value of the seed; if you want a different sequence, start with a different seed) To generate the next number, the generator updates the value of the seed, and returns it (or some part of it): seed = F(seed); return seed; The function F can take many forms. If it has the form F(x) --> (a * x + c) % m ... for some constants a , c , m , then this is a “linear congruential” generator: it computes a linear (or affine) function of the seed, results taken congruent with respect to (i.e., modulo) m

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 4 of 21 CSE 8A, UCSD LEC 16 The java.util.Random class The Random class in the java.util package implements a linear congruential pseudorandom number generator. It has two constructors: /** Create a Random object with the given seed. */ public Random(long seed) { . .. } /** Create a Random object, seeded with the system clock. */ public Random() { this(System.currentTimeMillis()); } In many cases, using the current system clock time is a good way to ‘seed’ the generator: java.util.Random r = new java.util.Random(); Now that we’ve created a Random object, how can we use it to generate random numbers? Use its instance methods!
Page 5 of 21 CSE 8A, UCSD LEC 16 java.util.Random instance methods /** Returns the next pseudorandom, uniformly distributed int * value from this random number generator’s sequence. */ public int nextInt() /** Returns the next pseudorandom, uniformly distributed * double value between 0.0 and * 1.0 from this random number generator’s sequence. */ public double nextDouble()

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 21

lec16 - CSE 8A: Lecture 16 Random number generation...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online