{"id":375,"date":"2018-06-28T08:47:04","date_gmt":"2018-06-28T08:47:04","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=375"},"modified":"2024-03-28T13:18:30","modified_gmt":"2024-03-28T13:18:30","slug":"random-number-generator","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/examples\/random-number-generator\/","title":{"rendered":"Random Number Generator in Excel"},"content":{"rendered":"\n
Randomness is a lack of pattern or predictability in events. It can be observed in nature, for example in the thermal change or decay of radioactive material. Computers, on the other hand, are things that are designed to eliminate randomness from the results because we want them to be predictable.<\/p>\n\n\n\n
For that reason, we can\u2019t really create a list of true random numbers using Excel or any other computer application. What we really mean by saying random, when talking about computers, is a pseudo-random number, a number that is generated using a specific pattern.<\/p>\n\n\n\n
To generate a list of pseudo-numbers we need a so-called \u201cseed\u201d. The seed is a number that is used to initialize a pseudo-random number generation.<\/p>\n\n\n\n
The repeatability of a pattern is determined by the length of the seed used in the process.<\/p>\n\n\n\n
For example, if you use a two-digit seed, the algorithm can produce at most 100 numbers before repeating the cycle. A three-digit seed will expand this number to 1000, 4 digits to 10000, and so on.<\/p>\n\n\n\n
Take a look at these two bitmaps. The first one is generated using the random.org<\/a> website. The site offers true randomization because it uses data generated by a natural process, such as atmospheric noise.<\/p>\n\n\n\n The second one uses the PHP rand() function. The seed number is not long enough, so we can observe the repeating pattern.<\/p>\n\n\n\n First, take a look at advanced pseudo-random generation and then I will show you how you can use a bit of the VBA code to generate real random numbers.<\/p>\n\n\n\n The most popular way to generate a pseudo-random number is by using the RAND()<\/strong> function. This function doesn\u2019t use any parameter and returns a decimal value between 0 and 1.<\/p>\n\n\n\n It means: Generate a random number between 0 and 1. Then multiply it by 65 minus 18 (which symbolize the maximum and minimum numbers). Now, the generated value is a number between<\/a> 0 and 47. Let\u2019s add a minimum number of years, which is 18.<\/p>\n\n\n\n Each time you press F9<\/span><\/strong> you will get a sequence of pseudorandom numbers:<\/p>\n\n\n\n 29.54275<\/p>\n\n\n\n 59.36941<\/p>\n\n\n\n 42.83664<\/p>\n\n\n\n 21.53878<\/p>\n\n\n\n Of course, no one will say that he has 29,54275 years. Let\u2019s use the ROUND()<\/strong> function to make integers from these numbers.<\/p>\n\n\n\n Press F9 to generate a list of random numbers between 18 and 65.<\/p>\n\n\n\n 38<\/p>\n\n\n\n 37<\/p>\n\n\n\n 52<\/p>\n\n\n\n 59<\/p>\n\n\n\n The easier and faster method to achieve the same result is by using the RANDBETWEEN function(). This function will return an integer value that is between two arguments: the lowest and the highest number. For our example, it will be:<\/p>\n\n\n\n Volatile and static numbers.<\/p>\n\n\n\n The RAND<\/strong> and RANDBETWEEN<\/strong> functions are volatile. It means that each time you change anything in your worksheet the value is recalculated. There are two simple ways you can work around this problem.<\/p>\n\n\n\n If you want your cell not to be recalculated even if you use the F9 key, you have to use VBA.<\/p>\n\n\n\n First, press Left Alt + F11<\/span><\/strong> to open Visual Basic Editor, then create a new module<\/a> in the project window and enter this code:<\/p>\n\n\n\n The code will generate a number. It works the same way as Excel\u2019s RAND<\/strong> function, so the numbers will change every time you change anything in the worksheet.<\/p>\n\n\n\n Change this line:<\/p>\n\n\n\n To this one:<\/p>\n\n\n\n Now, when you generate a new number, the value will stay the same. You can also drop this line because of the Application. The volatile procedure is, by default, set to false.<\/p>\n\n\n\n If you want to generate a list of numbers, you can use the AutoFill <\/a>feature the same way you use it for normal Excel functions.<\/p>\n\n\n\n In this example, we will randomly assign workers to work on a project for the next 10 days. This is what the example looks like.<\/p>\n\n\n\n Now, we will try to fill the cells from B2<\/strong> to B11<\/strong> with random names from the Name<\/strong> column. In order to do this, we will use the following formula.<\/p>\n\n\n\n It will randomly select one name from cells E2<\/strong> to E4<\/strong> (absolute cell reference) and insert it into cell B2.<\/strong> Use the AutoFill<\/a> feature to fill the remaining cells.<\/p>\n\n\n\n Press F9<\/strong> to generate a different combination.<\/p>\n\n\n\n I will show you an example you can use to sort a list of values in Excel in a random way.<\/p>\n\n\n\n Suppose you have a list of names of friends you want to call, but you cannot decide which one to call first. In this difficult situation, you decided to sort the names in a random order.<\/p>\n\n\n\n First, fill out the names.<\/p>\n\n\n\n Now change FORMULAS >> Calculation >> Calculation Options<\/strong> to manual and enter =RAND()<\/strong> in cell B1.<\/strong> Fill the rest of the cells (B2:B10<\/strong>), then sort the table by column B. Because we changed the Calculation Options to the manual, the values generated by the RAND<\/strong> function won\u2019t recalculate.<\/p>\n\n\n\n To use this tool, first, you need to install the add-in, called the Analysis ToolPak. Go to FILE >> Options >> Add-Ins<\/strong>. Select Analysis Toolpak and click Go\u2026<\/p>\n\n\n\n A new window will appear.<\/p>\n\n\n\n Check Analysis Toolpak and click OK.<\/p>\n\n\n\n When you do this, a new position will appear in the ribbon, in the data tab<\/a>. Click the Data Analysis button in DATA >> Analysis<\/strong>. In the new window click \u201cRandom Number Generation\u201d.<\/p>\n\n\n\n The new window called the Random Number Generation<\/strong> will appear:<\/p>\n\n\n\n In the Number of Variables<\/strong> you can enter the number of columns and in the Number of Random Numbers <\/strong>the number of rows.<\/p>\n\n\n\n The following types of distributions are available in Analysis Toolpak:<\/p>\n\n\n\n The uniform distribution will create random numbers between entered values. In our case, these will be numbers between 10 and 40. This method is similar to the RANDBETWEEN<\/strong> function.<\/p>\n\n\n\n Normal distribution means that most of the numbers are close to average while relatively few examples to the one extreme or the other. When you have enough numbers this distribution will generate a \u201cbell curve\u201d chart.<\/p>\n\n\n\n Here, we have two textboxes: mean<\/a><\/strong> and standard deviation<\/strong><\/a>.<\/p>\n\n\n\n Let\u2019s create two examples, one with a mean that equals 1 and the second one with a mean that equals 3. The mean will represent the average height of men. Let\u2019s suppose that the standard height for men is 180 cm (about 6 feet).<\/p>\n\n\n\n We will create 10000 numbers in one row. Fill text boxes as shown in the image below.<\/p>\n\n\n\n Sort the results. In cell B1<\/strong> enter the following formula.<\/p>\n\n\n\n Click the bottom right corner of the cell to fill in other cells.<\/p>\n\n\n\n Select all the cells and choose INSERT >> Charts >> Recommended Charts<\/strong>. Excel chose a scatter chart. It will be great for the data. Because we have 10 thousand numbers it will look like a line chart<\/a> in such a small space. But as you can see we have one extreme to the left, so we can see this is a scatter chart.<\/p>\n\n\n\n Change the mean to 3, generate 10 thousand results and see how the chart differs from the previous one.<\/p>\n\n\n\n It may seem steeper than the previous one. But take a look at the scale of the x-axis.<\/p>\n\n\n\n The results in both charts are between 150 and 200. Now change the scale. To do this, right-click on the x-axis and select the format axis.<\/p>\n\n\n\n Now, as you can see the higher the mean, the flatter the chart.<\/p>\n\n\n\n A good example of Bernoulli distribution is tossing a coin. You have a 50% chance of throwing each side of a coin. So in this example, you set the probability to 50% (0.5).<\/p>\n\n\n\n Michael Jordan\u2019s average effectiveness of free throws was 83.5% (.835). Set this parameter in the text box to generate a series of numbers.<\/p>\n\n\n\n You will get a series of numbers where about 83.5% will be successes (1) and about 16.5% failures (0).<\/p>\n\n\n\n The more numbers you generate the result will be closer to this number.<\/p>\n\n\n\n In the Bernoulli distribution, I wrote that the more numbers you generate, the result will be closer to the p Value<\/strong> you chose. In Binomial distribution, you can set two values, the p Value, which is the probability of success, and the number of trials.<\/p>\n\n\n\n We use coin flipping as an example. We use 10 trials and generate 6 results.<\/p>\n\n\n\n Because we used only 10 tries, the results are far from 50%.<\/p>\n\n\n\n Now let\u2019s set the number of trials to 100.<\/p>\n\n\n\n Now the numbers are much closer to 50%- between 46% and 54%.<\/p>\n\n\n\n In the last example let\u2019s use 10000 trials.<\/p>\n\n\n\n Because we used a lot of tries the numbers are much closer to 50%- between 49.71% to 51.34%.<\/p>\n\n\n\n First, let\u2019s talk about the POISSON<\/strong> function. This function will predict the probability of occurring the exact number of events.<\/p>\n\n\n\n Look at the following example:<\/p>\n\n\n\n Let\u2019s say that a restaurant on average sells 6 meals an hour. You want to know what is the probability of selling a particular number of meals.<\/p>\n\n\n\nRandom bitmap<\/h3>\n\n\n\n
Pseudo-random bitmap<\/h3>\n\n\n\n
RAND() function<\/h2>\n\n\n\n
=RAND() * (65-18)<\/pre>\n\n\n\n
=RAND() * (65-18) + 18<\/pre>\n\n\n\n
=ROUND(RAND() * (65-18) + 18, 0)<\/pre>\n\n\n\n
RANDBETWEEN() function<\/h2>\n\n\n\n
=RANDBETWEEN(18, 65)<\/pre>\n\n\n\n
\n
\n
Randomize the list in VBA<\/h2>\n\n\n\n
Function normalRand()\nApplication.Volatile True\nnormalRand = Rnd\nEnd Function<\/pre>\n\n\n\n
Application.Volatile True<\/pre>\n\n\n\n
Application.Volatile False<\/pre>\n\n\n\n
Random Selection<\/h2>\n\n\n\n
Example 1: Randomly assign employees<\/h3>\n\n\n\n
=INDEX($E$2:$E$4, RANDBETWEEN(1,3))<\/pre>\n\n\n\n
Random Sort<\/h2>\n\n\n\n
Example 2:<\/h3>\n\n\n\n
The Random Number Generation tool<\/h2>\n\n\n\n
Uniform distribution<\/h3>\n\n\n\n
Normal distribution<\/h3>\n\n\n\n
Example 3:<\/h3>\n\n\n\n
=NORM.DIST(A1,180,1,FALSE)<\/pre>\n\n\n\n
Example 4:<\/strong><\/h3>\n\n\n\n
Bernoulli distribution<\/h3>\n\n\n\n
Example 5:<\/h3>\n\n\n\n
Example 6:<\/h3>\n\n\n\n
Binomial distribution<\/h3>\n\n\n\n
Example 7:<\/h3>\n\n\n\n
Example 8:<\/h3>\n\n\n\n
Example 9:<\/h3>\n\n\n\n
Poisson function<\/h3>\n\n\n\n
Example 10:<\/h3>\n\n\n\n