Selecting random numbers in Excel. Excel Random Number Generator in Functions and Data Analysis

Random numbers are often useful in spreadsheets. For example, you can fill a range with random numbers to test formulas, or generate random numbers to simulate a wide variety of processes. Excel provides several ways to generate random numbers.

Using the RAND function

Function provided in Excel RAND generates a uniform random number between 0 and 1. In other words, any number between 0 and 1 has an equal probability of being returned by this function. If you need random numbers with large values, use a simple multiplication formula. The following formula, for example, generates a uniform random number between 0 and 1000:
=RAND()*1000 .

To restrict the random number to integers, use the function ROUND:
=ROUND((RAND()*1000);0) .

Using the RANDBETWEEN function

To generate uniform random numbers between any two numbers you can use the function CASE BETWEEN. The following formula, for example, generates a random number between 100 and 200:
=RANDBETWEEN(100,200) .

In versions earlier than Excel 2007, the function CASE BETWEEN Available only when installing an additional analysis package. For backward compatibility (and to avoid using this add-on), use a formula like this: A represents the bottom, a b- upper limit: =RAND()*(b-a)+a. To generate a random number between 40 and 50, use the following formula: =RAND()*(50-40)+40 .

Using the Analysis ToolPack Add-in

Another way to get random numbers in a worksheet is to use the plugin Analysis ToolPack(which came with Excel). This tool can generate uneven random numbers. They are not generated by formulas, so if you need a new set of random numbers, you need to rerun the procedure.

Get access to the package Analysis ToolPack by selecting Data Analysis Data Analysis. If this command is missing, install the package Analysis ToolPack using the dialog box Add-ons. The easiest way to call it is to press Atl+TI. In the dialog box Data analysis select Random number generation and press OK. A window will appear as shown in Fig. 130.1.

Select the distribution type from the drop-down list Distribution, and then set additional parameters (these vary depending on the distribution). Don't forget to specify the parameter Output interval, which stores random numbers.

Function RAND() returns a uniformly distributed random number x, where 0 £ x< 1. Вместе с тем путем несложных преобразований с помощью функции RAND() you can get any random real number. For example, to get a random number between a And b, just set the following formula in any cell of the Excel table: =RAND()*( b-a)+a .

Note that starting with Excel 2003, the function RAND() has been improved. It now implements the Wichman-Hill algorithm, which passes all standard tests for randomness and guarantees that repetition in a combination of random numbers will begin no earlier than after 10 13 generated numbers.

Random number generator in STATISTICA

To generate random numbers in STATISTICA, you need to double-click on the variable name in the data table (in which you are supposed to write the generated numbers). In the variable specification window, click the button Functions. In the window that opens (Fig. 1.17), you need to select Math and select a function Rnd .

RND(X ) - generation of uniformly distributed numbers. This function has only one parameter - X , which specifies the right boundary of the interval containing random numbers. In this case, 0 is the left border. To fit the general form of the function RND (X ) into the variable specification window, just double-click on the function name in the window Function Browser . After specifying the numeric value of the parameter X need to press OK . The program will display a message indicating that the function was written correctly and will ask for confirmation about recalculating the value of the variable. After confirmation, the corresponding column is filled with random numbers.

Assignment for independent work

1. Generate series of 10, 25, 50, 100 random numbers.

2. Calculate descriptive statistics



3. Construct histograms.

What conclusions can be drawn regarding the type of distribution? Will it be uniform? How does the number of observations affect this conclusion?

Lesson 2

Probability. Simulation of a complete group of events

Laboratory work No. 1

Laboratory work is an independent study followed by a defense.

Lesson objectives

Formation of stochastic modeling skills.

Understanding the essence and connection of the concepts “probability”, “relative frequency”, “statistical definition of probability”.

Experimental verification of the properties of probability and the possibility of calculating the probability of a random event experimentally.

- Formation of skills for studying phenomena of a probabilistic nature.

The events (phenomena) we observe can be divided into the following three types: reliable, impossible and random.

Reliable name an event that is sure to occur if a certain set of conditions is met S.

Impossible an event that is known to not occur if a set of conditions is met S.

Random call an event that, when a set of conditions S is fulfilled, can either occur or not occur.

The subject of probability theory is the study of probabilistic patterns of mass homogeneous random events.

Events are called incompatible, if the occurrence of one of them excludes the occurrence of other events in the same trial.

Several events form full group, if at least one of them appears as a result of the test. In other words, the occurrence of at least one of the events of the complete group is a reliable event.

Events are called equally possible, if there is reason to believe that none of these events is more possible than the others.

Each of the equally possible test results is called elementary outcome.

Classic definition of probability: probability of an event A they call the ratio of the number of outcomes favorable to this event to the total number of all equally possible incompatible elementary outcomes that form the complete group.

A is determined by the formula,

Where m– the number of elementary outcomes favorable to the event A, n– the number of all possible elementary test outcomes.

One of the disadvantages of the classical definition of probability is that it does not apply to trials with an infinite number of outcomes.

Geometric definition probability generalizes the classical one to the case of an infinite number of elementary outcomes and represents the probability of a point falling into a region (segment, part of a plane, etc.).

Thus, the probability of an event A is defined by the formula , where is the measure of the set A(length, area, volume); – measure of the space of elementary events.

Relative frequency, along with probability, belongs to the basic concepts of probability theory.

Relative frequency of the event call the ratio of the number of trials in which the event occurred to the total number of trials actually performed.

Thus, the relative frequency of the event A is determined by the formula, where m– number of occurrences of the event, n– total number of tests.

Another disadvantage of the classical definition of probability is that it is difficult to indicate the reasons for considering elementary events to be equally possible. For this reason, along with the classical definition, they also use statistical determination of probability, taking the relative frequency or a number close to it as the probability of an event.

1. Simulation of a random event with probability p.

A random number is generated y yp, then event A has occurred.

2. Simulation of a complete group of events.

Let us number the events that form a complete group with numbers from 1 to n(Where n– number of events) and draw up a table: in the first line – the event number, in the second – the probability of the occurrence of an event with the specified number.

Event number j n
Probability of event

Let's divide the segment into axis Oy points with coordinates p 1 , p 1 +p 2 , p 1 +p 2 +p 3 ,…, p 1 +p 2 +…+p n-1 on n partial intervals Δ 1 , Δ 2 ,…, Δ n. In this case, the length of the partial interval with number j equal to probability p j.

A random number is generated y, uniformly distributed on the segment. If y belongs to the interval Δ j, then event A j it has arrived.

Laboratory work No. 1. Experimental calculation of probability.

Goals of work: modeling of random events, studying the properties of the statistical probability of an event depending on the number of trials.

We will carry out laboratory work in two stages.

Stage 1. Simulation of a symmetrical coin toss.

Event A consists in the loss of the coat of arms. Probability p events A equal to 0.5.

a) It is necessary to find out what the number of tests should be n, so that with a probability of 0.9 the deviation (in absolute value) of the relative frequency of the appearance of the coat of arms m/n from probability p = 0.5 did not exceed the number ε > 0: .

Carry out calculations for ε = 0.05 and ε = 0.01. For calculations, we use a corollary from the Moivre-Laplace integral theorem:

Where ; q=1-p.

How are the values ​​related? ε And n?

b) Carry out k= 10 episodes n tests in each. In how many series is the inequality satisfied and in how many is it violated? What will be the result if k→ ∞?

Stage 2. Modeling the implementation of the outcomes of a random experiment.

a) Develop an algorithm for modeling the implementation of an experiment with random outcomes according to individual tasks (see Appendix 1).

b) Develop a program (programs) to simulate the implementation of the outcomes of the experiment a certain finite number of times, with the obligatory preservation of the initial conditions of the experiment and to calculate the frequency of occurrence of the event of interest.

c) Compile a statistical table of the dependence of the frequency of occurrence of a given event on the number of experiments performed.

d) Using the statistical table, construct a graph of the frequency of an event depending on the number of experiments.

e) Compile a statistical table of deviations of the frequency values ​​of an event from the probability of occurrence of this event.

f) Reflect the obtained tabular data on graphs.

g) Find the value n(number of trials) so that and .

Draw conclusions from the work.

To select random data from a table, you need to use function in Excel “Random numbers”. This is ready random number generator in Excel. This function is useful when conducting a random check or when conducting a lottery, etc.
So, we need to hold a prize draw for customers. Column A contains any information about customers - first name, last name, number, etc. In column c we set the random number function. Select cell B1. On the “Formulas” tab in the “Function Library” section, click on the “Mathematical” button and select the “RAND” function from the list. There is no need to fill out anything in the window that appears. Just click on the “OK” button. Copy the formula by column. It turned out like this.This formula places random numbers less than zero. In order for random numbers to be greater than zero, you need to write the following formula. =RAND()*100
When you press the F9 key, the random numbers change. You can select the first buyer from the list each time, but change random numbers with the F9 key.
Random number from a rangeExcel.
To get random numbers within a certain range, set the RANDBETWEEN function in the mathematical formulas. Let's set the formulas in column C. The dialog box is filled out like this.
Let's indicate the smallest and largest number. It turned out like this. You can use formulas to select first and last names of customers from a list with random numbers.
Attention! In the table, we place random numbers in the first column. We have such a table.
In cell F1 we ​​write a formula that will transfer the smallest random numbers.
=SMALL($A$1:$A$6,E1)
We copy the formula to cells F2 and F3 - we select three winners.
In cell G1 we write the following formula. She will select the names of the winners using random numbers from column F. =VLOOKUP(F1,$A$1:$B$6,2,0)
The result is a table of winners.

If you need to select winners in several categories, then press the F9 key and not only will the random numbers be replaced, but also the names of the winners associated with them.
How to disable random number updating inExcel.
To prevent a random number from changing in a cell, you need to write the formula manually and press the F9 key instead of the Enter key so that the formula is replaced with the value.
In Excel, there are several ways to copy formulas so that the references in them do not change. See the description of simple methods for such copying in the article "

We have a sequence of numbers consisting of practically independent elements that obey a given distribution. As a rule, uniform distribution.

You can generate random numbers in Excel in different ways and ways. Let's consider only the best of them.

Random Number Function in Excel

  1. The RAND function returns a random, uniformly distributed real number. It will be less than 1, greater than or equal to 0.
  2. The RANDBETWEEN function returns a random integer.

Let's look at their use with examples.

Sampling random numbers using RAND

This function requires no arguments (RAND()).

To generate a random real number in the range from 1 to 5, for example, use the following formula: =RAND()*(5-1)+1.

The returned random number is distributed uniformly over the interval.

Each time the worksheet is calculated or the value in any cell in the worksheet changes, a new random number is returned. If you want to save the generated population, you can replace the formula with its value.

  1. Click on the cell with a random number.
  2. In the formula bar, select the formula.
  3. Press F9. AND ENTER.

Let's check the uniformity of the distribution of random numbers from the first sample using a distribution histogram.


The range of vertical values ​​is frequency. Horizontal - “pockets”.



RANDBETWEEN function

The syntax for the RANDBETWEEN function is (lower bound; upper bound). The first argument must be less than the second. Otherwise the function will throw an error. The boundaries are assumed to be integers. The formula discards the fractional part.

Example of using the function:

Random numbers with precision 0.1 and 0.01:

How to make a random number generator in Excel

Let's make a random number generator that generates a value from a certain range. We use a formula like: =INDEX(A1:A10,INTEGER(RAND()*10)+1).

Let's make a random number generator in the range from 0 to 100 in steps of 10.

You need to select 2 random ones from the list of text values. Using the RAND function, we compare text values ​​in the range A1:A7 with random numbers.

Let's use the INDEX function to select two random text values ​​from the original list.

To select one random value from the list, use the following formula: =INDEX(A1:A7,RANDBETWEEN(1,COUNT(A1:A7))).

Normal distribution random number generator

The RAND and RANDBETWEEN functions produce random numbers with a uniform distribution. Any value with the same probability can fall into the lower limit of the requested range and into the upper one. This results in a huge spread from the target value.

A normal distribution implies that most of the generated numbers are close to the target number. Let's adjust the RANDBETWEEN formula and create a data array with a normal distribution.

The cost of product X is 100 rubles. The entire batch produced follows a normal distribution. A random variable also follows a normal probability distribution.

Under such conditions, the average value of the range is 100 rubles. Let's generate an array and build a graph with a normal distribution with a standard deviation of 1.5 rubles.

We use the function: =NORMINV(RAND();100;1.5).

Excel calculated which values ​​were within the probability range. Since the probability of producing a product with a cost of 100 rubles is maximum, the formula shows values ​​close to 100 more often than others.

Let's move on to plotting the graph. First you need to create a table with categories. To do this, we divide the array into periods:

Based on the data obtained, we can generate a diagram with a normal distribution. The value axis is the number of variables in the interval, the category axis is periods.

Good day, dear reader!

Recently, the need arose to create a kind of random number generator in Excel within the boundaries of the required task, and it was simple, taking into account the number of people, select a random user, everything is very simple and even banal. But I was interested in what else can be done with the help of such a generator, what they are, what their functions are used for this and in what form. There are a lot of questions, so I will gradually answer them.

So, what exactly can we use this mechanism for:

  • Firstly: to test formulas, we can fill the range we need with random numbers;
  • Secondly: to generate questions for various tests;
  • Thirdly: for any random distribution of tasks in advance among your employees;
  • fourthly: for simulating a wide variety of processes;

…… and in many other situations!

In this article I will consider only 3 options for creating a generator (I will not describe macro capabilities), namely:

Creating a random number generator using the RAND function

Using the RAND function, we have the ability to generate any random number in the range from 0 to 1 and this function will look like this:

=RAND();

If the need arises, as it most likely does, to use a large random number, you can simply multiply your function by any number, for example 100, and you will get:

=RAND()*100;
But if you don't like fractions or just need to use integers, then use this combination of functions, it will allow you to follow the decimal point or just discard them:

=ROUND((RAND()*100);0);

=RESULT((RAND()*100);0)
When there is a need to use a random number generator in a certain, specific range, according to our conditions, for example, from 1 to 6, you need to use the following construction (be sure to secure the cells with ):

=RAND()*(b-a)+a, Where,

  • a – represents the lower bound,
  • b – upper limit

and the full formula will look like: =RAND()*(6-1)+1, and without fractional parts you need to write: =RESULT(RAND()*(6-1)+1;0)

Create a random number generator using the RANDBETWEEN function

This function is simpler and began to please us in the basic version of Excel, after the 2007 version, which greatly simplified the work with the generator when it is necessary to use a range. For example, to generate a random number in the range from 20 to 50, we will use the following construction:

=RANDBETWEEN(20,50).

Create a generator using the AnalysisToolPack add-on

The third method does not use any generation function, but everything is done using an add-on AnalysisToolPack(This add-in is included with Excel.) The tool built into the table editor can be used as a generation tool, but you need to know that if you want to change the set of random numbers, then you need to restart this procedure.

To gain access to this undeniably useful add-on, you first need to use the dialog box "Add-ons" install this package. If you already have it installed, then the matter is small, select the menu item “Data” – “Analysis” – “Data Analysis”, select from the list offered by the program and click "OK".

In the window that opens, we select the type from the menu "Distribution", then we indicate additional parameters that change based on the type of distribution. Well, the final step is this indication "Output Interval", exactly the interval where your random numbers will be stored.

And that's all for me! I really hope that I fully explained the question of creating a random number generator and everything is clear to you. I would be very grateful for your comments, as this is an indicator of readability and inspires me to write new articles! Share what you read with your friends and like it!

Don't think too much. This is how you create problems that weren’t there in the first place.

Friedrich Nietzsche