Posted on

Learn MATLAB Episode #24: Generating Random Values

So in this class we’re going to talk about generating a random variable from a certain distribution. This could be useful for doing simulations of systems that have uncertainty. Matlab has some built-in functions to help us do this. So the first one we’re going to talk about is called randi, and it takes one argument called imax, and this function gives us a uniformly distributed variable between one and imax. So let’s try it. So 9 is in between 1 and 10. Now there’s another function randi which takes in a maximum value, and another parameter called n. So let’s set n to 3, so that returns an n-by-n matrix of random values between 1 and imax. So suppose I wanted to generate random values between 10 and 20, how would we do that? Because randi can give us values anywhere between 1 and imax, so what we could do is we could just add 10 to all the values that randi returns. So this gives us a 3 by 3 matrix with values only between 10 and 20. Another useful function is just rand by itself. So this function gives us a random number between 0 and 1, so it’s different from the previous one where we don’t get integers we get real numbers. rand returns a number with a uniform distribution, so the probability of getting point .25 is the same as the probability of getting 0.75. So let’s try and imply histograms for different values of n. Alright, so this is a histogram for random numbers between 0 and 1, and N equals 10 array. Alright, so it’s not quite uniformly distributed, let’s try a bigger n value. Alright, so immediately it starts looking more uniformly distributed as n increases, so let’s try a bigger n. Alright, so it looks even more uniformly distributed. Now, 10,000. Alright, so it’s almost flat even. That’s 100,000, and this is a million, it looks almost perfectly flat. So that’s the idea with the frequentist view of probability is that when n approaches infinity, your probabilities approach their true values. So now let’s think about a different problem. Suppose I want a specific discrete distribution, so say I want to simulate an unfair coin. So, to write it out I want p of heads equal to .25, and I want p of tails to equal 0.75. How could I write a function to give me random values that could draw from this distribution instead of a uniform distribution? So we can create a function to do this. We can call it biased coin, it’s going to take in one value little p which represents, let’s call it P heads which is probability of getting heads, and it’s going to return the coin face. So we’re going to generate a random value, if it’s less than P heads we’re going to return heads, else we’re going to return tails. Let’s try our function. Alright, so now we’re going to try our new biased coin function by initializing an array of say size 1,000…you know what we’re going to do this in a separate function. We’re going to initialize a n by 1 array, we’re going to count from 1 to n, and we’re going to use the biased coin function to generate a value for each element of the array. Alright, so let’s try the function we just made. Test coin .25 for n equal to 1,000. Alright, so you see the number of heads which resolves to the integer 104, and then tails resolves to the integer 116. So you see this is about 250 and this is about 750 which is what we would expect in the thousand coin tosses.