 write a program to generate 10 random numbers that we have there were permutations just means that numbers are arranged in a random order okay so every time when you run the program it's going to generate a list of 10 numbers from 1 to 10 and arrange different orders randomly and you should not have duplicates as we unique numbers between 1 and 10 so how would how will we do this and what is the best approach to it okay so the idea is that you're going to have a list of 10 slots and then you're going to go to the first slot and add a number there right the proof was approached will be you go to the first slot right you're the first one so you check here you check check if this is empty right if it's empty then there's no data in there yet that state that's like the case number one it's like case I guess case number one if it's empty they all course the first number could be any number between 1 and 10 right it doesn't really matter so then you if it's that's the case then you add one to the first right so here you would before we do that let's create a list first so put it down a list is equal to empty right if it's blank there's no data in there yet and so we check the list so we say if the numb list numb list is actually the Len right so Len of the numb list is equal to zero that means that there's no data there yet so it's gonna be the first number you're going to generate now I do want to mention one thing here we didn't cover this yet but there is a library that you need to import into Python to use it and that's called random so up here on the very top of your code you want to import a library called random okay and we'll do this next time how you can use that your own module it's important to your program and use it so this random here has another function to call random dot run int for example I'll do it here to show you so you can see it and this is a really good way to use it so I'm gonna import random first okay and there's a function called random that run and your passing number you can see that there are two parameters the A and B it just mean the minimum and the maximum so it's a range between let's say one and five okay if you want to generate random between one and five that is going to give it to you okay so if I go up there okay run again you see the every time I hit that it's gonna randomly generate a number of it for me between one and five okay so that's how you use it to generate a random number it's gonna we're gonna fill this list from one to ten all right so I'm gonna put here a R for the number for the random number is gonna be random dot run int want the integer between one and ten right that's this is between one and ten okay so initially you would you run it you might get a one you might get a ten I don't know but if the list is empty I know that I can go ahead and add that to the num list we use to append the function okay that always add to the end of the list so I'm gonna add the R to that list so now I got my first number okay that only works for the first case one the list list is empty after that we don't know anymore right it's gonna be have one number it might have all ten we don't know and so so from here on then we're going to maybe use a loop right use a loop and the loop can generate a from one to ten or from I guess one tonight that's how you do it okay and so if it's not so if it's not zero then we need to generate those numbers okay so that means we're gonna start a loop for every number from let's say what should we call it from I up to and the range of one up to ten right because we already have the first one so we just need a one to ten with this which is 99 numbers left so that means like the second slot right so so in this case after we did that one so our num will look like for example if the random is two then it looks like that okay then the next time we check is no longer two so what do we do okay we will then check to see if it's a if it's a if it's the number is in the list or not right if it's in there then you want to generate a random number again until we get a random number so before I do this though I want to put this into a loop because we'll loop this to the whole thing ten times so actually I should up here I want to do is put a loop up here let's do a here let's do a range actually this should be up there it should be up here I probably should but if I for range between one all the one to ten which is ten right including zero which is the index for now and I don't like ten here right it's a magic number so we don't like it so usually you put it max max is ten right you put max here and then you hear from zero to max so don't use the magic magic number in your in your code and they would have and did this over like this right so before we do that though I want just to randomly generate the number first maybe I should move that up here like this I generate a random number first I check to see if the list is empty now if it is empty then go ahead and add that to the list okay I forgot my code in here if it's not empty that means the list has some data already they haven't got to go ahead and do the following so you have what do you have to do actually I'm not gonna do that one here yet I already did up here so I'm not gonna do a loop again so that means that it's possible that the number could have like two five eight and already but I don't know in there so what I do is you can check to see if this random number is in the list right so you say if the R is in the list of num list right remember that one a function so for example if it's if the list is one two three right if I say is two and if three and L it will return true right if it's not in the L if I put 12 and you return false okay so if it's already in there that means that I don't want to add the random number again I have to generate a different random number again right has to be different so that may if it's in the list then you basically continue you can do this continue do nothing right skip it and go back to the loop again but if you do that this loop is gonna go to the next one as you you might skip the next slot must skip it so you don't want to skip it I'm stuck so right I'm stuck at the first number I'm looking at the second position and the second position I check it and say do we have a number already if I randomly generate two again oh with two is already in there okay so I have to go back and get a regenerate another number but I don't want to keep looping this all the way to 10 yet so that means that actually loop this whole thing I'm inside another loop right so I'm doing a brute force here so that means I have to do something like while something is true I can force it to be true and then move that over okay so at the first index I'm looking at the first lot and if it's true yes continue the loop here I generate a random number I say I got it too thanks I got it too I check my list for the very first time it's empty so it is zero okay that's good then add to or push to to the list of num num list now has to so this else plot is completely skipped goes back to the L goes back to the wall and then this whole thing repeats again so I don't want that either right I want to be able to jump out of the while loop and go back to the next index so it goes to the next position okay so that means once I append it here then I have to break out of that loop okay this means exit the while loop so exit the while loop you completely get out of that and then there's nothing out here so we got to go to the next index now I'm looking at number so initially zero now look in a position at one okay so the index one comes in the whole thing repeats again I generate another number let's say this time I got it three okay the three goes in here I check its part no this is no longer true so this whole thing will be skipped it's only runs at the very first time I need to check that anyway and then it goes down here so I check this R is this R which is three is three in that list my list was only two so no it's not it's not true right so therefore this is not true so I don't continue I will go to the else close because it's not in the list that means that I'm gonna go ahead and add this to my list so I go to num list that append the R okay and then once I do that again I have to break out of that right because once I found it then I have to break out of that okay and then now we go in an index I'm just do here I'll stop here index number two so now I go here and I run a regenerate number let's say this time I got a two again okay it's a random number right between one and ten so down here this is false we skip this we go to else close is two and the list and we already had two and three now by now we had two and three right so yes two is in the list okay so two is in the list so therefore this is true you're gonna continue continuing means skip everything else and go back to the while loop and go back here again and then we generate render another number again what if I get it to again it's possible right so you see this cycle through until I get a number that's no longer two and I get a let's say I get it seven and then our seven comes in here this is false yet it's not in there it's new number add that to this list so my number now has three seven I break out the while loop okay and then now you go to index number three right so you see how this is done this is a really tedious process it's called a brute force approach because you go through every iteration the entire list of ten numbers and you randomly generate a number from one to ten is unique you place it in there so we'll see this works okay I'm gonna go down here and save it and let's run and let's see if and at the very end over here I'm gonna print out print out my numbness and see what that looks like okay so you can run this and here's my numbness as you can see is unique from one to ten there's no duplicates in there but run again I should get a different permutation right it's randomly a different okay so here I put one up to ten I should have put here up to max right max is ten so now I can do more I can add like what if I go up to 50 right so you can see that by changing using this kind of variable it's very easy to change one place and your program works just like that so if I go and run and now I'm gonna get 50 numbers and this should all be unique okay so this is the what's called the brute force approach because I'm going through my list I'm adding one at a time but every time before I add a number I have to randomly generate a number between one and max and if it's already in the list I have to keep you know cycling this every time until I get a different number I can add it to the list so it's possible that I keep getting a two many times so wasting a lot of CPU time right we don't know because that's really fast you can tell so I want to show you an approach that is asked by the the author so what I want to do is going to put this into a put it to a function we call it def it's called it F1 for now so I move everything over okay and if you want to use it you can call it down here I can call so to use it you have to call F1 and then invoke that function right so you can get the result out here okay so now I'm gonna do another one this time we do the F2 using the one that is given by the book and or something similar so the author says let's go back over here use this approach okay this one here that does something like this and why is this important efficient what we'll just check it out so it says to make a second list and fill the list with numbers from one to ten right so we keep this num list as as it is I'm gonna fill another list so it says I'm gonna go here create a list we call it we should call it numbers I guess and this number is going to be filled with a number from one to ten right so one if you're just gonna be one to ten instead of doing that way I want to do a four look really quick for I and range from one up to max right brushy yes zero up to max max as as you can see it's orange so my variable is kind of bad right it's a keyword but that's fine for now and I'm gonna generate a number from nums append the i plus one okay so I'm basically generate another one one two and then up to max okay that is number so one to ten one to a hundred and so forth so that is my second list this is the thing here now repeat ten times as you can see here so we do a range or I I and range up to ten we'll call it ten call it max right whatever that is so it says remove the element at the position of the second list every move means you can use a remove function you can use the pop function a lot of the functions we talked about last time so for example if my list is go to one three five like this right to remove like the first position you can do a l that removed and then remove a value a value of one right remove the one and now my list is only three and five left so every time I take out a number I reduce the size by one okay so once I reduce it the number is gone from the list so I don't have to worry about random numbers right it's already it's already it's already there so my my other list will always be unique all right that's one option another option you can do is you can do a l that pop if you just do pop here it will always pop the last one the list I don't want that I want to pop a number that is in there so and the number of the index position of the number so let's say three it's not possible because three is not an index my l has only two so zero and one only so the pop function would do l dot pop a position of one will give you five if it turns the number also it removes from the list okay so my it pops of five returns that number and then it drops up from the list and then my list is down to one okay so we use the function I'll add this number that is pop from that list I pinned that to my num list okay and so here I go into randomly general in number the number will be the index of this num's list for one two whatever it is right so here you would randomly generate a number like like like we did before and so I'll do here or again random that random int between zero okay up to now max okay if you do that it will always generate a number between you know up to max we don't want that the size number that when we when we remove something this size shrinks by one shrink by one every time so we want to do is the length of the num's right this num number now it has whatever that many x if it's 10 it has size of 10 I've removed one it's going to be size of nine size of eight seven six and so forth so when the random number is going to be from one actually uh yeah from zero up to that number minus one because it's the index position right the index is always one less than the length so this is this is the index between zero and n minus one right so I gather my number position this is a random position of r and then what I do is I'm going to just basically it says remove that element from that list so if you do something like this say for the n is equal to nums that remove if I remove but again I have to randomly get a number but I don't want that number I want to get the position of that so again that's why I say I'll use the pop at the r position okay if r is the index position of these numbers so if this is like you know it's going to be like one one two three four five and then all the way to uh n right 100 doesn't matter okay so it says the index of whatever this random is let's say it's random of three so here goes zero one two this is the position of the three third index right so I'm going to pop this number four out assign the number to the n at the same time pop will remove this from the list okay so I take that out and then I append that to the num list the n to the list and it pops so that at this point this has been reduced by one okay so the number is now gone from the list because four is out it's going to be one two three and then five two the rest of those numbers right and then I would do it again so you go back and you go to the next index position and the next one right of the range or whatever that is you do it again get a random position if I get a three again this has a one zero one two three so I pop that number out five say the two n so five is removed from the list and then now I add it to the list and so forth so now I'm dealing with the position only um and I will always get a unique index because my index is always shrunk by one right so eventually it's going to get to zero okay and that's when we stop so this is the approach that is given here and so if I go back down here and run the f2 function and if it's correct I should get the same result as before save it and run it oops okay so I get a random number again just like before from one two fifty I can make it smaller so we can see um those ten digits okay so here you go it's unique just like the other one okay so as you can see both works which is fine but now is like which one is more efficient right and that is why uh program is it's so intriguing it's so fun because you can have this you know code can be done in many different different ways and what is the best way how do you measure that yeah just so you want example to do that you can you can see why sometimes one is better than the other okay so to measure it I'm going to go up here and in a random minute get another library called time so I will measure the time before I call the function so right here I'm going to start here okay the function up here is not really important is it only affects what I call this function or this function here okay so it will go in here do its own thing and then it will exit out right okay so so before I let let's try f1 first okay before I run it I want to set called t1 is equal to time time I'll start here it will set the clock and then it will go and run the f1 process and then when it gets out I get different times they'll call it t2 okay and then I get my list and then I'm going to go also here print the call it um f1 and we'll get the difference between the two right it's it's basically t2 minus t1 right we'll give you the difference in time right so so for example let's just see what happens right here okay run okay so the time is like zero zero it's really really fast right you can't really tell because it's way too fast now what I'm going to do is it also changes to um a format let's put a percent here uh we'll go to maybe like 15 decimal places okay and we'll do just make sure it's subtraction first uh up to 15 decimal places so you can see the number it it's still zero zero not too bad right it's because number is too small what happens when I reduce or changes to let's say 100 okay the size of 100 numbers now let's see what it looks like okay you can see that the time's consuming now instead of zero zero zero zero zero zero zero nine nine okay what if I ever change it to let's say 500 okay zero zero eight not bad what if I change it to a thousand okay so point zero four right so you can see it's it's not that bad right it still goes and still does it uh quite fast now it's lagging okay ten thousand it still don't take so it took me four seconds okay so four seconds for a ten thousand number now let's try the second one try the f2 okay the same number of numbers and I'll run this boom right point zero zero nine seconds at ten thousand numbers so you can see this is way more efficient than the f1 okay so you can see both f1 and I'll run f2 here and I'll have a time three is equal to time dot time and then we'll duplicate this this is the f2 times would be t3 minus t2 okay so we save compare the two and you will see that it's running right now so here we go the f1 takes about four point eight seconds the f2 is only point zero one seconds okay so you can see the result is the same but the code is makes it so more efficient using the second approach right so sometimes when you program yeah do the brute force first make sure it works once it's working they can go back and revise it to make it more efficient