BYU logo Computer Science

Umm (random frequency) Transcript

Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.

[00:00:00] Instructor: Howdy! Let’s show you how to use random in the umm activity. So we want to write a program that takes a frequency, a number between zero and one, and a phrase as command line arguments. And then we’re going to randomly inject umm into a given sentence at the given frequency and print the result. Let’s go fix this. There we go, given frequency and we’re going to print the results. So, let’s go set that up and then see what we need to do next.

[00:00:34] We have an empty file. We don’t like empty files, so let’s make a little name here. If name is main, call me. All right. Great. So we know that our program needs a frequency and a phrase. So in our main function kind of embodies the program, let’s take a frequency and a phrase.

[00:01:03] And then to get those in, we’re going to have to pull those out of the command line arguments. So we’re going to need to import sys and let’s let the first one be the frequency. All the arguments in the sys argv come in as strings, but if the frequency is going to be useful as a number, we can turn it into a float. So we’ll say float.

[00:01:30] Sys argv one is our first argument. And then the phrase we can just take a sys argv two. Great. So now our program’s set up. Now what was the problem statement? We’re going to take that frequency in that phrase. We’re going to randomly inject something at a given frequency and print the result.

[00:01:51] Well, we’ve seen that pattern presented in the guide where you do something at a given frequency. You’re iterating through things and you’re checking if a random number is less than your frequency and if it is you take action. So if we’re going to inject into a string, into a sequence of words, that’s the opposite effect of the filter pattern, right? We’re going to create a new list, but it has more things in it than the original list did.

[00:02:24] But we can start with that idea to set things up. We know we’re going to be doing random stuff. So let’s import random and let’s say, we create umm phrase with our frequency and our phrase. And then I’m going to just print that out. That’s the basic structure of our program.

[00:02:52] Now, the create umm phrase is going to be like a filter pattern but different, right? So we’re going to have new phrase and let’s make this a list. So we want to treat the phrase as a sequence of words. So let’s create a list of words, a new list of words that will join back together with spaces at the end.

[00:03:16] So now I can say for word in phrase dot split. Great. Now I’m going to new phrase dot append to that word. And then if, and this is the frequency pattern, right? So if random dot random number, so random dot random, this will give us a number between zero and one at random. If that’s less than the frequency, then we’re also going to take a phrase and append umm into it.

[00:03:54] And at the end, we’ll return our new phrase joined together with spaces. That gives us a new phrase. So here we see the frequency pattern being used checking if random dot random is less than the specified frequency. What do we want to do in that situation? And we build up a new list here.

[00:04:16] All right, let’s try this out. Let’s come back to the current lecture is on the 22nd. All right, there is umm dot py. So I should be able to say Python umm dot py. We need a frequency, let’s say 0.2, one out of five words.

[00:04:42] And then because I want to pass a phrase in and the phrase is going to have spaces in it, I need to use quotes on the command line in order to do value single quotes. And so what’s some phrase? Hello, how are you? What is your name? Do you like tacos? All right, try this. Hello, how umm are you? What is umm your name? Do you umm like tacos? Great.

[00:05:17] We now can come take this and we could push this up higher. Eight, right? Yeah. Hello, umm how umm are umm you? umm What umm is umm your name? Do umm you like umm tacos? Funny, silly. The best one, you know, we’ve all been there before, right? Hello, would you like to go on a date with me?

[00:05:47] Eight’s probably not right for that. We’ve all been there, right? Here it comes out constantly. We could instead drop that down to maybe something a little bit more optimistic. But you get the idea, right? You can play with this parameter.

[00:06:07] If we drop it to zero, you don’t get any changes, right? If you push it up to one, then every word gets an umm in between because every time you append a word, you’re also going to append an umm. And so that’s the concept of how you can encode a frequency. And with that, have fun with it.