Excel Random number from a set of options - excel

In MS Excel, how can I randomly calculate a number number that is from one of a set of 5 options?
For example, in cell B1 I would like to have a random number that is either 15,30,50,75, or 100.
I would like a completely random output of these 5 numbers in cells B1:B100.
I was thinking that I could generate a random number in cell A1 using rand, then using a series of > or < IF statements to output only one of these numbers above.

This formula will do it:
=CHOOSE(RANDBETWEEN(1,5),15,30,50,75,100)
If you want to use a range of cells:
=INDEX($B$2:$B$6,RANDBETWEEN(1,5))

A quick and easy way would be to first make a lookup list like this:
Then in your column do a formula like this:
=VLOOKUP(ROUND(RAND()*10,0),$A$7:$B$16,1,FALSE)
where the $A$7:$B$16 is where your list is at. It can be on a different tab, or separate file, if you really need to isolate it.
You could also create a custom VBA function too, but I think that is beyond what you are looking for.

Let's say you have filled rows 1-5 in row G of a spreadsheet with the values you want to randomly display. You can use =INDIRECT("G"&RANDBETWEEN(1,5)) to display any one of those randomly.
`INDIRECT` lets you reference a cell using a string.
Since you want cells "G1" - "G5", we start of with "G".
& combines "G" with the value of the next function.
Then the RANDBETWEEN function will give us a number between the two parameters we provide (in this case 1 through 5).
Let me know if this helps :)

How about:
=SMALL({array containing numbers},RANDBETWEEN(1,COUNT({array containing numbers})))
e.g. if you have an array containing the 5 numbers you want to use in $B$2:$B$6
=SMALL($B$2:$B$6,RANDBETWEEN(1,COUNT($B$2:$B$6)))
This returns a random position in the list of numbers, with the total frequency of numbers being defined size of the array.

Related

Excel - Add an excel formula within FilterXML

I have an excel spreadsheet containing a list of strings in one column. The list of strings is made up of several numbers from varying lengths, separated by “/” and “;” The first entry of the string is a code id (which always has a length of 3)(red in example) followed by an “/” then an amount (which varies in length)(green in example) followed by an “;” if the string continues.
With the help of a member, I can now isolate the green number with the following formula:
=IF(ISBLANK(A4);"";TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A4;"/";";");";";"</s><s>")&"</s></t>";"//s[position() mod 2 = 0]")))
However, I still need an other formula that multiplies the green number with a variable, if a condintion is met.
Example function:
=IFS(B2<=10;B2*1,25;B2<=20;B2*1,18;B2<=100;B2*1,05;B2<=250;B2*1,01;B2>250;B2)
Is there a way to combine both these functions?
A very nice way of dealing with this is assigning the full array to a name, let's say a variable, through LET(). So your formula in B2 would become:
=IF(A2<>"",LET(MNT,TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A2,"/",";"),";","</s><s>")&"</s></t>","//s[position() mod 2 = 0]")),IFS(MNT<=10,MNT*1.25,MNT<=20,MNT*1.18,MNT<=100,MNT*1.05,MNT<=250,MNT*1.01,MNT>250,MNT)),"")
It, however, does require Excel O365. But since you are transposing the array it appears you do have that.

Random number in Excel between 1 and 10 until all numbers are selected

I want to use Excel to generate random numbers between 1 and 10 and then count how many numbers were generated for each number to be generated.
I know how to generate random numbers, i.e. =RANDBETWEEN(1,10)
and then just pull down the corner.
But it's a bit of work to manually go through the list checking when all the numbers were generated.
Is there an easier/automatic way to do this ?
Assuming you have a header you could use:
Formula in A2:
=IF(SUM(--ISNUMBER(MATCH(ROW($1:$10),A$1:A1,0)))=10,"Done: "&ROW()-2&" numbers made",RANDBETWEEN(1,10))
Note that this is an array formula and depending on one's version of Excel you need to accept it through CtrlShiftEnter.
Drag it down, but remember that RANDBETWEEN is volatile and will recalculate each time you insert a new value.
If you have an existing list of random numbers in the range 1 to 10 and want to check it to see at what point all of the numbers in that range have been generated, you could use a formula like this:
=MATCH(10,MMULT(SIGN(COUNTIF(OFFSET(A1,0,0,ROW(1:100)),COLUMN(A:J))),ROW(1:10)^0),0)
assuming that there are up to 100 numbers.
Must be entered as an array formula using CtrlShiftEnter in less recent versions of Excel.
The idea is to use Offset to select an increasing number of rows from the range, then use Count to test for the presence of each number and Mmult to get the total of different numbers for each range. The use of the volatile function offset is normally deprecated, but since the sheet would already be using another volatile function, Randbetween, there may not be much additional effect on performance.

Taking average of certain values in one Excel column based on values in another

I have a (large) array of data in Excel of which I need to compute the average value of certain values in one column, based on the values of another column. For example, here's a snippet of my data:
So specifically, I want to take the average of the F635 mean values corresponding with Row values of 1. To take it a step further, I want this to continue to Row values of 2, Row values of 3 etc.
I'm not familiar with how to run code in Excel but have attempted to solve this by using the following:
=IF($C = "1", AVERAGE($D:$D), "")
which (to my understanding) can be interpreted as "if the values (anywhere) in column C are equal to 1, then take the average of the corresponding values in column D."
Of course, as I try this I get a formula error from Excel.
Any guidance would be incredibly appreciated. Thanks in advance.
For more complicated cases, I would use an array-formula. This one is simple enough for the AVERAGEIF formula. For instance =AVERAGEIF(A1:A23;1;B1:B23)
Array-formula allows for more elaborate ifs. To replicate the above, you could do =SUM(IF($A$1:$A$23=1;$B$1:$B$23;0))/COUNT(IF($A$1:$A$23=1;$B$1:$B$23;0)).
Looks like more work but you can create extremely elaborate if-statements. Instead of hitting ENTER, do CTRL-ENTER when entering the formula. Use * between criteria to replicate AND or + for OR. Example: SUM(IF(($A$1:$A$23="apple")*($B$1:$B$23="green");$C$1:$C$23;0)) tallies values for green apples in c1:c23.
Your sample data includes three columns with potential ifs so my guess is that you're going to need array formulas at some point.
Excel already has a builtin function for exactly this use; AVERAGEIF().
=AVERAGEIF(C:C,1,D:D)

Using VLOOKUP or INDEX/MATCH to run a Lottery

I'm using Excel to run a sort of lottery.
The spreadsheet columns are set up thus:
COL1:Person Name; COL2: Chosen Number A; COL3: Chosen Number B; COL4: Chosen Number C
There is then a set of data, generated using RAND() and ROUND, that gives 3 winning numbers, each between 0 and 10.
What I'm trying to do is identify a winner, by using VLOOKUP or INDEX/MATCH, or some combination, or other function, to identify the winning person, so that there is a single cell that returns the name of the winner.
The added complexity is that by looking up each of the numbers individually by column, an individual selection of, say, 1,4,8, isn't a winning selection against a randomly selection of say, 4,8,1.
Ideas?
You can concatenate numbers to additionalcolumn so it will contain string "1,4,8," and then perform a VLOOKUP for concatenated in the same way winning numbers.
By the way, this solution will show only first person, but isn't it possible that several persons guessed same numbers and won?
If you want to generate a 3-digit number, by far the easiest thing to do is to use the formula
=RANDBETWEEN(0,999)
You can select the cells and then enter (via the format dialog accessible by right-clicking) the custom format 000 if you want it to display as 3 digits so that e.g. 7 displays as 007. This will allow you to directly use VLOOKUP on a single value. #kipar asks an excellent question about potential multiple winners.
I implemented the abovementioned solution and it was quite easy. After your 4 columns, you add one column with
=TEXT(B1;0)&TEXT(C1;0)&TEXT(D1;0)
which combines the number to one string. Then you put your winning number in a cell of preference mine was M28 and the value was 123. After your first five columns you use the following formula.
=IF.ERROR(INDEX(A$1:A$4;SMALL(IF(E$1:E$4=TEXT($M$28;0);ROW(E$1:E$4));RI-OW());1);"")
The IF.ERROR is used to put a blank when there are no multiple winners. The index is used to get the winners out of your first column, that'w why there's a one at the end. The small is used to find the first occurence of the winner. You also have to enter it as an array formula so press ctrl+shift+enter instead of just enter when the formula is completed. I hope this answer is satisfying.
PS. For extra information on the use of this function go here: http://chandoo.org/wp/2014/12/09/multiple-occurrences-lookup-and-extraction/

Count max value (text+number) occurrences with filtering a specific part of text in excel

I'm having an excel column range (including blank cells) something like:
00EGB00-GE001
00EGB00-GE001
00EGB00-GE001
00EGB00-GE001
00EGB00-GE002
00EGB00-GE002
00EGB00-GE002
00EGB00-GE002
00EGD20-GD101
What I need is to Count total number of similar values and I'm stuck with the logic for counting total unique "similar" values... example "GE" & "GD" separately.
How to count total number of unique "GE" values in the list?
I thought =COUNTIF(B:B,"*GE*") should work but it does not. It gives total count of "GE" but I need to find unique count. Example GE001 & GE002 should be considered as 2 values in total.
Kindly help
EDIT AGAIN: Given further clarification below, and assuming that the data always has the same number of digits, one way to do it is by putting this in Column B:
=RIGHT(A1,5)
Then, if you have Excel 2007 or up, Copy and Paste Values and use Remove Duplicates to leave you with the unique values. Then remove the items with GD, either manually or using a formula.
In this case, the output is:
GE001
GE002
In this case, you can easily see that it's 2. If you have lots of values, you can use COUNTA. Is that what you want?
YET ANOTHER EDIT BASED ON LAST COMMENT: this is probably getting closer:
=SUMPRODUCT(--(MID(A1:A9,9,2)="GE"),1/COUNTIF(A1:A9,A1:A9))
Where the "GE" is hard-coded in the formula above you could also substitute a cell reference where you can alter the value.
Or, if you don't know where the text you want will be exactly because the number of characters change, this will work (but you'd need to be careful with what you were searching on because it might repeat somewhere else in the string):
=SUMPRODUCT(--(ISERR(SEARCH("GE",A1:A9))<>TRUE),1/COUNTIF(A1:A9,A1:A9))
Again, you can replace the "GE" with a cell reference.
As discovered below, though -- blank cells will cause this to fail. There IS almost definitely a way to cater for them (maybe using a FREQUENCY based Array Formula), but if you can live with cleaning out the blank cells then that would be one way of doing it.
LAST EDIT: this will account for blank cells. It is an Array Formula, and CAN be used on whole columns, but that will be quite slow as it takes up a fair bit of calculation effort:
{=SUMPRODUCT(--(MID(A1:A9,9,2)="GE"),IF(ISBLANK(A1:A9),1,1/COUNTIF(A1:A9,A1:A9)))}
As it's an Array Formula, use Ctrl + Shift + Enter to input it.

Resources