{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14-440-127+Lecture+05

14-440-127+Lecture+05 - 14:440:127 Introduction to...

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

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, you’ve probably seen me create ‘random’ matrices whenever I want to demonstrate a concept on the fly. How do you create ”random” numbers in Matlab? There’s 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 we’ll want random values in an interval [0,b], where b is not 1. Well, let’s say we have random numbers on the unit interval [0,1]. If we multiply those numbers by b, we’ll 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 won’t test you on it. So why have I been putting the word ‘random’ in quotes? Because these numbers aren’t actually random. They’re generated with a Pseudorandom Number Generator (PRNG), deterministically (which means you’ll get the same result each time you run the process). Pseudorandom means that they ‘look’ or ‘seem’ random (i.e. they meet some statistical tests about the uniformity of their distribution, or perhaps it’s difficult, when given all of the numbers

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.

{[ snackBarMessage ]}

Page1 / 12

14-440-127+Lecture+05 - 14:440:127 Introduction to...

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

View Full Document
Ask a homework question - tutors are online