This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 05 Rutgers University, Spring 2010 Instructor- Blase E. Ur 1 (Pseudo)random Number Generation In previous lectures, youve probably seen me create random matrices whenever I want to demonstrate a concept on the fly. How do you create random numbers in Matlab? Theres an app rand() function for that. In short, Matlab lets you create matrices of pseudorandom numbers between 0 and 1. However, from this primitive, you can generate numbers in any interval using some arithmetic. rand(m,n) creates an m x n matrix of random values and rand(s) creates an s x s matrix of random values. In technical terms, the values in this matrix are decimals uniformly distributed on the unit interval [0,1]. That means the probability of getting 0.1 more or less equals the probability of getting 0.2, and so on. Of course, sometimes well want random values in an interval [0,b], where b is not 1. Well, lets say we have random numbers on the unit interval [0,1]. If we multiply those numbers by b, well effectively stretch the interval and now have random values randomly distributed on the interval [0,b]. Thus, b*rand(m,n) creates an m x n matrix of decimals on the interval [0,b]. What if you instead wanted an m x n matrix of values uniformly distributed on the interval [a,b]? Well, first notice that the size of this interval is (b-a), and thus we use the trick above. (b-a)*rand(m,n) creates a random value on the interval [0,b-a]. Now, add a to the number you created, shifting the interval to the right on the number line by a . Therefore, r = a + (b-a).*rand(m,n) returns an m x n matrix of decimals on the interval [a,b]. What if you instead wanted an m x n matrix of random integers from 1 to x? Create an m x n matrix of random decimals on the interval [0,x] and apply the ceiling function: r = ceil(x.*rand(m,n)); . Pay close attention to the fact that the original interval started at 0, but applying the ceil function rounds all of the numbers like 0.001 up to 1, so the integers range from 1 to x. Similarly, to create random integers from A to B, we notice that r = ceil(x.*rand(m,n)) creates integers in the range [1,x], noting that the size of this range is (x-1). Thus, to create a range of integers of size (B-A), we do r = ceil((B-A+1).*rand(m,n)) . Since we want to shift the beginning of this range from 1 to A, we shift by (A-1). Thus, r = ceil((B-A+1).*rand(m,n))+(A-1) generates an m x n matrix of random integers between A and B, inclusive. 1.1 PSEUDOrandom Note that this section is for students who are interested; I wont test you on it. So why have I been putting the word random in quotes? Because these numbers arent actually random. Theyre generated with a Pseudorandom Number Generator (PRNG), deterministically (which means youll get the same result each time you run the process). Pseudorandom means that they look or seem random (i.e. they meet somemeans that they look or seem random (i....
View Full Document
This note was uploaded on 03/20/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.
- Spring '08