 Welcome to this course on data structures and algorithms. Today, we will be revisiting an example that we had considered earlier namely simulating the Mumbai-Vada power restaurant. But this time, we will add the concept of random number generators to simulate random arrival of people, random order placement, etcetera, etcetera. Let us look at the concept of random number generation to begin with. So, as I mentioned in the previous example, we simply assumed a certain time instances when the customer will arrive, when the customer will place an order, the number of items ordered, etcetera. In practice, we know that such events will occur randomly. For example, when we throw a dice, we get a random value between 1 and 6. We do not keep getting 1, 1, 1, 2, 2, 2, 2, 2, etcetera. So, we do not know which will be the next value. Of course, we know that on an average, if we throw the dice for example, for 600 times, we will get 100 times, 100 times, 2, etcetera, etcetera. Such a property incidentally is called uniformly distributed random numbers in which every number can occur exactly the same number of times amongst all the possibilities. Now, it is possible to simulate our events by generating random numbers. These are called pseudo random numbers because these are generated algorithmically. C plus plus provides functions to generate such random numbers. So, let us look at the random number generation. Consider a dice. If we throw the dice, as I said, we will get sometimes 2, sometimes 6, sometimes 1. The function ran is used to generate uniformly distributed random number. Every time you call this function, it will return one number between 0 and a parameter called a ran underscore max. This in a sense is the largest positive integer number that can be reprinted on the machine. Of course, a very large number. So, anytime you generate a number, you will get any number between 0 and such a large number. But suppose we want to generate numbers between 1 and 6 to simulate the throw of a dice, we could write the small program segment in which I have defined R as a random number, dice as a number that will come out for the dice randomly and some index R. In this loop, I am generating 10 random numbers. Every time I call ran, a new random number R will be generated, which will be between these two values. When I take module of 6 of that R and add 1 to it, I will automatically get a number between 1 and 6. This is just output to show what I will get. For example, if I execute this program segment, I will get this sequence. Unfortunately, if I run this program again, I will get the same sequence, because the ran function starts with a predetermined value called seed value to generate the sequence of random numbers. Obviously, if you want simulate real life situations, then you would like to generate different sequences on different occasions. You can actually do that by using a new function called s ran. s stands for seed and it initializes the random number generator with a seed. Of course, if you give the same seed, it will still generate the same sequence. The trick is to give a different seed value to ensure that a different sequence is generated. A typical value that is used to ensure that a different seed is given is simply to use the current time for which there is a function called time. If you say time 0, the computer system will return the current time. Since that will be different every time, this function call will ensure that a different seed is given to the random number generator. Observe for example, the output from two different executions, where we initialize the seed using this statement. So, one sequence is 4, 2, 6, 5, 5, 1, 6, 1, 1, 2. If you rerun the program, next time you get 1, 2, 1, 3, 1, 3, 6, etcetera, you can actually try out by running that program segment yourself. Equipped with this kind of random number generator, we shall now see how we can simulate the arrival of people, order placement, etcetera by actually using random numbers to simulate the real life experience. We revisit the data structures of that example, you are all familiar with that. For example, food info structure, customer info structure, customer queue structure, etcetera, etcetera. They are all standard structures that we had used earlier. We want to maintain the information about order place and the orders that go to kitchen. So, you have order structure. Now, this time it contains all the information of the order. Please note time for placing order is now to be calculated randomly. Similarly, for kitchen, which contains the information stored in the structure order. Additionally, it will also have preparation and fulfillment time of order. You will recall that last time we had assumed some arbitrary preparation time. This time we will use random number generators to simulate a real life experience. Look at the functions, load menu, set food items, all of these are same. Customer arrives is also the same function. It pushes the information of the customer on the queue, but please note that the arrival time that it includes now will be generated randomly placing an order. Now, here we elaborate a bit more than what we had done last time. First we generate the token ID, which is a standard sequential next number. Then we randomize the number of items ordered, the item IDs that are ordered and quantity for each item that is ordered. Once these numbers are generated, the function will calculate the cost of each item in that quantity and the total cost. Next, we will again use randomization to calculate the time for placing order. We send this order information to kitchen and the customer exists from that queue, waits on a table for order to be fulfilled. Here is order goes to kitchen function. It loads information about order placed in customer, but it calculates preparation time for the order and then calculate the order fulfillment time. It then updates the list in kitchen list which we had already seen. Once again we note that the preparation time for the order will be based on some amount of randomization. Of course, it will be based on the number of items ordered etcetera. Finally, the dispatch orders function is very simple. It will sort the list in kitchen list based on the fulfillment time and it will dispatch orders. So, that the customers waiting on the table for the orders to be fulfilled in the sequence in which these orders are fulfilled, they will be delivered by the kitchen staff and customers can collect them. Please note that the total amount of time for which the customer has to wait is the time since the customer arrives and the time of the fulfillment of the order which will include all intermediate waiting times etcetera. Let us look at the computer program. Much of the program is exactly identical to what we had earlier seen. For example, I have customer info, I have stuck food info, orders and kitchen. Please note that for the stuck orders in addition to token ID, we now have number of items and then of course, for each item, the item code, the quantity and the cost and finally, the total cost. In addition, we also put inside the structure the placing order time which as I said should depend upon the number of items that have been ordered with some random value in between. Here is the class restaurant, the private members are caste ID, token ID, food index etcetera and then I have the food infrastructure. I have the list kitchen which is declared here. Now, I have public functions, load menu, set food items, customer arrives, place order and dispatch orders. So, the class is very simply similar to what we had earlier used. We will see the details of differences in the implementation. We initialize certain set values customer ID to 0, token ID to 1000 and initial food index to 0. This is the function which will generate random numbers. Note that just as we wanted to generate random number between 1 and 6 for our example of dice, we might want to generate random numbers between 2 different limits. So, here is an int low and int high and you will see how simply I generate the random number, take the modulo high minus low plus 1 and add low. I will exactly get a random number in between these 2 numbers which define the range. Consider set food items, it is exactly the same as we had seen earlier. Load menu exactly same as we had seen earlier. Now, customer arrives. Here is the difference that we make now. Of course, we add one to customer ID as usual whenever customer comes. The customer arrival time is set to time which is a parameter to this function. We push the customer on the queue and output that customer's information. Where is the randomization then? Wait. It will come as this parameter time. We shall see that in the rest of the program. Here is placing of order. The placing of order again I will add one to the token ID. I define the total cost and P time or placing order time. We need to write code to place the order to calculate placing order time and display order information of customer. This will be the function. In the next slide, we shall see code for these 3 different pieces. We just note that we have initialized the values. We have added one to the token ID and now we have to calculate all of these and put them into the order. So, first of all how to place order for number of items where the number of items could be less than or equal to 6. We make that assumption that nobody orders more than 6 items. So, we generate a random number between 1 to 6. This defines the random number of items which a customer orders. Next, we generate the starting item ID also randomly and then we generate an index which is also generated randomly. Now, what we do is for 0 to number of items that we have generated randomly, we will generate an item ID by simply adding item ID plus the index that we have generated and we will generate the quantity also randomly. We assume that everybody may order between 1 and 10 pieces of a particular item. Once that is done, we push the item ID into order item quantity into order quantity and then calculate based on the rate the total cost for that particular item. At the end, we will calculate the total cost which is updated every time we calculate the cost for an individual item. At the end of this iteration, I would have the total cost calculated which assigned to order dot total. To summarize, we have used random number generator to simulate the real life placing of order by a customer. Every customer will place order for different items for different quantities for which he will have to pay. Not only every person will order for different quantities etcetera, but depending upon the number of items that are ordered or the order placement time itself will differ, because the clerk at the counter will take longer to enter your order to calculate the total etcetera etcetera. So, here is the code to calculate placing of the order time. We start with the assumption that the number of items will determine the total time required to place the order. We take on an average 5 seconds for every item. We add a random aspect by saying that we generate a random number between this value and this plus 15. So, plus minus 15 seconds will be the extra time that may be required. This is exactly same as we had seen. All that we need to do is the p time is to be added to the customer dot q dot arrival time. The arrival time will be generated elsewhere when the customer comes to that we add the order placement time. If the list is empty, if it is not empty then now two possibilities exist. One, the next customer has not yet arrived or second the customer has already arrived and is waiting. Depending upon that we take the appropriate time to add the order placement time to it. Once we do that we will get the total order placement time that was required. This is simply display of the order information of the customer which is a very standard output state. Now, let us look at order going to kitchen. Please note that the customer has placed the order, the money has been collected and the clerk will simply inform the kitchen that such and such order has been placed. So, we have this kitchen, we have this p time, k dot order is set to order, k dot c info is set to the customer q information by looking at the front. The p time that is the preparation time now will depend upon the number of items ordered. Again we make an assumption that on an average 120 seconds are required to prepare one particular item. So, depending upon the total number of items order the total p time will be this. Of course, we would like to randomize it. So, we are generating a random number between this p time and p time plus 60. So, that the preparation time for the kitchen is actually randomly generated. Next we calculate the order fulfillment time. The order fulfillment time for a customer will be the time required to place the order plus the time required for preparing that. Once the order fulfillment time is calculated, this is by the way the time we note on which the order will be delivered out on the table by the kitchen. Once we have done all these calculations, we push back this k onto the list in kitchen and we just print it out. Please note that this will be done for every order that is placed and therefore, at the end of this simulation we will have all the orders that have been placed by all the customers who have visited Mumbai-Valapav shop. This is again well known. We have seen that already. This is dispatch orders thing which actually sorts the kitchen queue. Please note that we use a function pointer here which points to the comparison that has to be made in order to arrange or sort all the orders. But these orders are to be arranged in the order fulfillment time all. You will note that that is the time when the order will come out for delivery to the customer. We finally come to the main program. First and foremost, as I mentioned at the very beginning, every time we run this simulation, we would like a different sequence of timings at which customers arrive, different sequence of timings during which customers place the order, etc. So we use the srand function here. The restaurant is MVP restaurant and MVP restaurant dot load menu will load the menu. We have seen that already. We are simulating five customers here. So the customer arrive time is customer arrive plus a random number generated between 1 and 20. Once that is done, we add the customer arrives by calling this restaurant dot customer arrives and we give this time as the arrival time. Next, the customer will place an order. Now the arrival times are already captured. When I invoke place order, it will use that information and randomly generate the placement of order, etc. and it would have completed all the orders that are placed by these five customers who have come. Once this is done, I invoke the dispatch order which involves the sorting of the kitchen list and everything and finally I will dispatch all the orders. Please note that by simply simulating this for 500 instead of 5, I could simulate a situation where 500 customers are actually simulated. Please also note that the information about arrival time, about the order placement time, about the order fulfillment time is all available. It will be very easy for you to calculate the average waiting time for the customer. For example, in the queue to place an order or average waiting time after the order is placed, but before it is fulfilled or the total average waiting time for the customer. Once customer arrives in the restaurant till the time that customer receive the fulfilled order. You can see that by simply using a random number generator available in C plus plus, we have been able to convert our example of the Vada Pav restaurant into a randomly generated realistic sequence of customer arrival, customer placing orders, etc. In addition to the standard feature of list which is available in C plus plus, we have also used in this example the random number generator which is also available. Please note that we have used only uniformly distributed random numbers. Those of you who are interested in random number generation belonging to different probability distributions may wish to read more information available and other functions which are available in C plus plus. Thank you.