Excel Formula/Function to Subtract randomly - excel

I'm rather new to excel and I want to use excel or perhaps another program to subtract on a fixed amount but randomly.
It is like n1 + n2 + n3 = 300
But I want n1 and n2 and n3 to be different numbers hence not division
Examples
150 + 75 + 75 = 300
or
100 + 100 + 100 = 300
or
50 + 100 + 150 = 300
A function to subtract a fixed amount but random subtraction
I'm still quite confused on how to do this on excel, sorry for my bad English and explaination
Please help.

If it is always 3 numbers:
The first number we use:
=RANDBETWEEN(1,A1)
Then the second:
=RANDBETWEEN(1,A1-B1)
Then the third is just the remainder:
=A1-B1-C1

In excel you have your numbers in rows and columns
I would enter 100, 100 , 100 in separate columns of the same row and then use this
formula
=(A1+B1+C1).
Meaning row A1 100, row B1 100, row C1 100. The answer should be 300
if you want to subtract just use the minus sign instead of the plus sign:
formula =(A1-B1-C1).

Try:
=LET(A,RANDBETWEEN(0,300),B,RANDBETWEEN(0,300-A),HSTACK(A,B,300-SUM(A,B)))
Or, if no HSTACK() available:
=LET(A,RANDBETWEEN(0,300),B,RANDBETWEEN(0,300-A),CHOOSE({1,2,3},A,B,300-SUM(A,B)))
I'm just unsure if pure mathematically this is as random as can be. Would it be 'more random' if one would list all possible permutation of 1-300 that would add up to 300 and randomly pick a solution from this list?

One way to approach this if you want two numbers to always add up to the same value, is to modify each value with the same amount, by adding to one value and subtracting from the other
100 = n1 + n2 = (n1+a) + (n1-a)
where a is any random number.
To extend this to three numbers, you can use two artbitrary numbers a and b to do this following
100 = n1 + n2 + n3 = (n1+a) + (n2-a+b) + (n3-b)
The simplified approach to this it to pick completely random n2 and n3 and let n1 pick up the difference
100 = (100-n2-n3) + n2 + n3
To do this in Excel, use the =RANDBETWEEN() function for n2 and n3 and then for n1 just subtract from 100
=100 - SUM(n2,n3)

Related

Excel: averageif function for more than one cell

I have data like this:
A B C D E
1 6 7 8 9 1
2 1 2 3 4 5
3 N2 N4 N3 N5 N1
All the values are associated with numbers (N1, N2 ...). I know how to get one value by number using SUMIF:
=SUMIF($A$3:$E$3; "N1"; $A$2:$E$2) ' will get "5"
But I have another task: I need to get average of last m values before value founded with SUMIF (or AVERAGEIF).
For example for m=3, N = N1 and row = 2: we'll find number "5" and get average of last three values in row before N1: (5 + 4 + 3) / 3 = 4
How could I do it correctly?
In case you do not have Excel O365, you could use:
Formula in L2:
=SUMPRODUCT(INDEX(A1:E2,I3,ROW(INDEX(A:A,MAX(MATCH(I2,A3:E3,0)-I1+1,1)):INDEX(A:A,MATCH(I2,A3:E3,0)))))/I1
A bit lenghty, but it will also work if you use N4 or N2 while you can have "m=3".
If one has O365, you can use:
=SUM(INDEX(A1:E2,I3,SEQUENCE(I1,,MATCH(I2,A3:E3,0)-I1+1)))/I1
That's made on the assumption you will always have "m" values available.
Another option:
=SUMPRODUCT(A1:E2*(ROW(A1:E2)=H3)*(COLUMN(A1:E2)<=MATCH(H2,A3:E3,0))*(COLUMN(A1:E2)>(MATCH(H2,A3:E3,0)-H1)))/H1
I propose alternate solution (supposing that first column is A)
dataset
Function used for result
=AVERAGE(INDIRECT(CHAR(MATCH($H$2,$A$4:$E$4,0)+65-$H$3)&ROW()&":"&CHAR(MATCH($H$2,$A$4:$E$4,0)+64)&ROW()))
Check this for diff N and m
OR
If your data doesn't start with A always you have to tweak the values for CHAR() i.e. 64 and 64+1=65 only.

Get Sum of Products in same column

I have the below data set:
Let's assume it starts in A1. I would like to calculate the Total Costfor January by taking each Count and multiplying by the Rate below it.
I currently have the following, nonsensical formula:
Total Cost = (B1 * B2) + (B3 * B4) + (B5 * B6) + ... + (Bn + Bn+1) for n > 0
Is there a whizzy way to do this using an Excel formula? Perhaps something with SUMPRODUCT()? But I can't seem to get that to work the way I need it...
Simplified example:
=SUMPRODUCT(--(A1:A5="Count"),B1:B5*B2:B6)

Calculating contrast values on Excel

I am currently studying experimental designs in statistics and I am calculating values pertaining to 2^3 factorial designs.
The question that I have is particularly with the calculations of the "contrasts".
My goal of this question is to learn how to use the table "Coded Factors" and "Total" in order to get the values "Contrast" using the IF THEN function in Excel.
For example, Contrast A is calculated as : x - y . Where
x = sum of the values in the Total, where the Coded Factor A is + .
And y= sum of the values in the Total, where the Coded Factor A is - .
This would be rather simple, but for the interactions it is a bit more complex.
For example, contrast AC is obtained as : x - y . Where
x = sum of the values in the Total, where the product of Coded Factor A and that of C becomes + .
And y = sum of the values in the Total, where the product of Coded Factor A and that of B becomes - .
I would really appreciate your help.
Edited:
Considering the way how IF statements work, I thought that it might be a good idea to convert the + into 1 and - into -1 to make the calculation straight forward.
Convert all +/- to 1/-1. Use some cells as helper..
Put in these formulas :
J2 --> =LEFT(J1)
K2 --> =MID(J1,2,1)
L2 --> =MID(J1,3,1)
Put
J3 --> =IF(J$2="",1,INDEX($B3:$D3,MATCH(J$2,$B$2:$D$2,0)))
and drag to L10. Then
M3 --> =J3*K3*L3*G3
and drag to M10. Lastly,
M1 --> =SUM(M3:M10)
How to use : Input the Factor comb in cell J1 and the result will be in M1.
Idea : separate the factor text > load the multiplier > multiply Total values with multiplier > get sum.
Hope it helps.

Dynamic Programming. Coin Row Problem - How its Recurrsive relation is developed

Problem: A set of n coins is placed in a row. The coins have positive values which need not be distinct. Find the maximum amount that can be collected given the constraint that no two adjacent coins can be picked up.
Its recursive relation is
F(n) = max{cn + F(n − 2), F(n − 1)} for n > 1,
F(0) = 0, F(1) = c1.
My question is how this recursive relation is developed. Please someone explain this to me.
First, envision a line of coins, with the value of each depicted by the variable ci:
c1 c2 c3 c4 c5 ... cn
If there are no coins, than obviously the max amount that can be made is 0. Likewise, if there is only 1 coin, the max amount is the value of that coin, c1. This accounts for the base case.
For the recursive case of the max value for n coins, start at cn, which is the right-most coin. Since the constraint is that you cannot select adjacent coins, the max value that you can achieve is either the right most coin plus the max achieved from 2 slots to the left (this accounts for the f(n - 2), or the max achieved by selecting the coin immediately to the left (accounting for the f(n - 1) case) and discarding the rightmost coin cn.
Considering the following line of coins again:
c1 c2 c3 c4 c5 c6
The f(6) case would look at c6 + the greatest amount from coins c1 - c4, OR the greatest amount from coins c1 - c5 (and excludes c6).
f(4), likewise, returns c4 + the greatest amount from coins c1 - c2, OR the great amount from coins c1 - c3 (again excluding c4).
f(2) returns c2 + c0 or the greatest amount from c1 (effectively c1) The first equates to c2, since c0 is 0 by the base case, and the second equates to c1 (again by the base case). So f(2) is really just the max of c1 or c2.
Note, too, that the f(n - 2) and f(n - 1) may be the same, since in the n - 1 case it might be beneficial to select the coin to the left (which is the f(n - 2) case). But that is why the first half is not merely f(n - 2), but also adds to it cn
Let's start at the end.
Let's denote the answer to the problem of n coins as F(n)
If you have zero coins, the amount is zero. so F(0) = 0.
If you have a single coin, the amount is that coin's value, so F(1)=c1
Now suppose someone told you the values of F(n-1), F(n-2). How can you use them to find F(n)?
If you have n coins, you have two possible moves:
Pick the nth coin, skip the adjacent one ((n-1)th coin, that's the rule!) and resume solving from there.
Skip the nth coin, and resume solving from the adjacent (n-1)th.
How do you express the notions of 1 and 2 with the tools you have?
If you pick the nth coin, it's value is Cn. Now you have to skip the (n-1)th coin, and continue solving from (n-2) coin. This is Cn + F(n-2).
If you skip the nth coin it contributes 0 to the solution, and now you resume solving from the (n-1)th coin. That's F(n-1).
Which one of either case 1 or case 2 is larger? You don't know. But you can express it as
max(Cn + F(n-2), F(n-1)),
which is saying "I don't which one is larger, but one of them is so return it please".
I hope this helps!
Let F(n) be the maximum amount that can be picked up from the row of n coins.
To derive a recurrence for F(n), we partition all the allowed coin selections into two groups:
those that include the last coin and those without it.
The largest amount we can get from the first group is equal to cn + F(n − 2)—
the value of the nth coin plus the maximum amount we can pick up from the first n − 2 coins.
The maximum amount we can get from the second group is equal to F(n − 1) by the definition of F(n).

using excel divide any value in a col by 1000, then multp new value by 10 & upd a running total cell, while leaving the orig value alone

For example if I enter a 2000 in B3, I would like that number divided by 1000, then multiplied by 10, and have the new value added to a running total. ie (2000/1000 * 10=20)
RunningTotal = 20
For clarity, if I enter 8000 in B4, then I would like to (8000/1000 * 10 = 80 )
RunningTotal = 100
Notice that
(x / 1000 * 10) + (y / 1000 * 10) = (x + y)/1000 * 10
So the equation for your running total cell only needs to be:
=SUM(B3:B10)/1000*10
Assuming B3:B10 is the appropriate range for your inputs.
equation formula
(x / 1000 * 10) + (y / 1000 * 10) = (x + y)/1000 * 10
=
(x+y) * 0.01
=
sum(B3:B?) * 0.01
You can simply record a macro:
http://office.microsoft.com/en-us/excel-help/create-a-macro-HP005204711.aspx
So you do everything you would like to calculate "by hand" while recording a macro and then you can call it at any time you want to execute.
Thanks for the replies.
I will be entering in values into col B(B3,B4...B200 etc.). The running total value will be displayed in it's own cell. The running total value will be updated upon save or enter. The orig value entered into the B column cells should remain the same.
The formula or calculation should occur anytime a value is entered into a cell within col B.
So if I have any number of values in col B, what I am expecting is:
1000/1000 * 10 = 10
5000/1000 * 10 = 60
8000/1000 * 10 = 140 etc.
If I use a separate cell to hold the runningTotal value, for ex K2...formula is: =C3/1000*10 ...how can I repeat this formula for subsequent cells in col B, but update the cell K2?
Must I use a holding column for each corresponding value entered in col B, then add the value to the K2 cell?
Ah, yes, I have managed to get it working by using a holding cell. if there is a more elegant approach, I am open to suggestions.
thanks for your advice.

Resources