Google Spreadsheets Repeat Function Nth Times & Sum Results - google-sheets

I have the following function
=IF(RAND()<0.25,1,0)
RAND() returns any value between 0 to 1 in decimal format and the idea is that an item has a 25% chance of getting a 1. If it was less than 0.25 the rand() then its a hit and gets a 1 otherwise a 0. Now lets say I need to do this 100 times and add up the sum of all the '1's that were created, which in this case will average to around 25 for 25%. How do I do this in Google Spreadsheets?
Basically looking for a way to repeat a function n'th amount of times and sum the results.
I have looked around everywhere (youtube, google forums) and have not found any solutions.

I may as well put this as an answer because it tries to address the broader question of whether you can repeat a function (say) 100 times. The answer is, yes if the function is compatible with an array formula. Rand can't be used in this way because it doesn't take any arguments (neither do some other functions like countifs for some reason). But you could get round it by using Randbetween instead and providing it with 100 array elements. These are multiplied by zero so don't actually affect the answer, but Google Sheets still evaluates the function 100 times:
ArrayFormula(sum(if(randbetween(0,A1:A100*0+99)<25,1,0)))
or
=Sumproduct(if(randbetween(0,A1:A100*0+99)<25,1,0))
The result is each time you force this to re-calculate (by changing something in the range A1:A100 or by setting File -> Spreadsheet Settings -> (Tab) Calculation -> Recalculation to every minute) it will give an answer around 25.
To make it more resilient (allow any value in A1:A100 including error values) could try
=ArrayFormula(sum(if(randbetween(0,iferror(A1:A100/0,0)+99)<25,1,0)))
or
=Sumproduct(if(randbetween(0,iferror(A1:A100/0,0)+99)<25,1,0))
I don't know why I didn't do this in the first place
=ArrayFormula(sum(if(randbetween(0,row(A1:A100)*0+99)<25,1,0)))
then this easily allows for a variable range
=ArrayFormula(sum(if(randbetween(0,row(indirect("A1:A"&H1))*0+99)<25,1,0)))
where the number in H1 doesn't have to be limited to the number of rows in the sheet.

Okay so I found a very convoluted answer. If someone finds a better please let me know.
The first thing as the user |'-'| commented was to create a range on separate sheet.
Since I know that I will not be looking up more than 200 values at once I created my range to be 200 long of this formula.
=IF(RAND()<0.25,1,0)
This will create the initial list of random values.
The next step is you need to generate a randomizer seed. Which is basically a random number between the range you created. You can do this with
=RANDBETWEEN(1,200)
This should be on the same column as what you are trying to sum up later.
Next you want to create a dynamic string that you can access via arrayFormula later.
="Randomizer!B"&B12&":B"&B12+B3
In my case I had the 200 random numbers on a sheet called randomizer. Notice the &, this is how you connect strings. In my example B12 is the reference to the =RANDBETWEEN(1,200), and B3 is how many times I want the randomness to occur. It can be any value as long as it's less than the randomizer seed by the amount of times you want it to be random.
Finally refer to this string using, =SUM(ARRAYFORMULA(INDIRECT(B13))) , indirect lets you refer to a string as a cell and this is how I was able to create a dynamic range to calculate from.
I will say the advantage of this method is its super fast to calculate since the random numbers have been pre-computed.
The idea is that it will keep creating random ranges from the precomputed random numbers you created, and then summing those ranges, essentially calculating random numbers n'th amount of times.
Hope this helps someone.

Related

Trying to only SUM values in one column based on if the adjacent cells in another column contain a formula

So I'm creating a Google Sheets doc that contains my hours and overtime for an hourly position I have, and how much overtime I'll need to make the amount of money I need to make to survive.
In addition to this I am also trying to track my hours so I can track the actual amount of money I'm making.
So I have a projected clock out time in Column B that uses a formula to tell me what time I need to ideally clock out at based on how many hours I need and what time I clocked in at. When I clock out, I enter the actual time I clocked out at.
In Column C I have the total amount of hours I worked that day, formatted in duration based on the difference between the value in Column A and Column B. If I haven't entered in a value in Column B, it shows me the ideal amount of hours I need that day.
I want to calculate my actual hours worked per week as I'm working that week, so I need to ONLY sum the values in Column C if the adjacent value in Column B is NOT a formula. So I need it to sum the values in column C if I've entered the actual time that I clocked out at in column B.
I've looked up this question and tried multiple solutions I've found here, even tried troubleshooting with ChatGPT, but most are just trying to sum the range that contains the values/formula, and not summing a different column based on if another column has formulas or not.
There seems to be a lot of posts that come super close but don't seem to work for how I need this to work.
Edit: Here is the example sheet.
So F3:F6 are values that have been manually entered, while F:7 has been calculated by a formula.
I need H9 to sum the values of H3:H7, but only the values adjacent to the times in the F column that have been manually entered. In this example, I need it to sum H3:H6 and ignore H7 until I enter a time and remove the formula in F7.
try:
=SUMPRODUCT(MAP(F3:F7,LAMBDA(z,IFNA(FORMULATEXT(z),1))),H3:H7)
You may filter and check if there is a formula with FORMULATEXT. If there isn't it will throw a #N/A error. Then with ISNA it will keep the values in which its adjacents did'nt have a formula:
=SUM(FILTER(C:C,BYROW(B:B,LAMBDA(b,ISNA(FORMULATEXT(o))))))

Having REPT function iterate over multiple values

Hi I'm curious if this is even possible and just thought I'd throw it out.
I have this function in google sheets:
=ArrayFormula(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(UNIQUE('Data Validation'!B3)&",",'Data Validation'!C3),,999^99),","))))
It allows me to specify the thing I want repeated as well as the number of times I want it repeated. Both of those are found in my data validation sheet which contains the following:
I am curious if there is a way to build the array formula so it essentially says:
repeat each element in column B the corresponding number of times in column C
Or in other words repeat EVP Sales 380 times then in the same output column repeat VP Inside Payments 36 times etc.
I haven't found anything remotely related to this online.
Thanks!
The formula you are using is already built for that scenario, just supply the needed range to calculate it properly
Formula:
=ArrayFormula(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(UNIQUE('Data Validation'!B2:B4)&",",'Data Validation'!C2:C4),,999^99),","))))
Output:

How to find optimal scores in a score database in Google Sheets?

I have a database of scores (C2:C937) which range from 4000-6500. I have the records for each score (D2:D937) and the differential between the 2 (E2:E937). I am trying to find a formula to tell me which score is closest to going over 5000. I was thinking to have a formula that checks the sums of the C column values and their corresponding E column values, and then sorting by which values have the most to improve. I don't know if it's possible to make a loop of sorts in sheets (kind of like a for loop in Java).
The image below it shows a set of values as explained above. The result I would be looking for would return whichever value has the highest chance of going over 5000, that is already the closest.
Here is a set of expected results. In this dataset, "Yanmega 4" would never be returned since it is already over 5000 in the C column. "Zangoose 1" and "Yanmega 3" would be first since they are close to going over 5000 and have enough leeway above 5000 (~400 points of leeway above it). "Zangoose 2" would be next since it has ~300 over 5000 meaning getting over 5000 should be easier than some of the others. "Zangoose 4" would be next since only 28 points need to be gained to get more than 5000, and there are 105 points of wiggle room as well. "Zangoose 3" is last since it only has 15 points of wiggle room based upon the current record.
Here is what I had started. The first line is convuluted and was my attempt at creating a loop. The second line has a good start I just don't know where to go from there. Both lines are unfinished.
=IF(IF(LARGE($C$2:$C$937,1)<5000,LARGE($C$2:$C$937,1),IF(LARGE($C$2:$C$937,2)<5000,LARGE($C$2:$C$937,2),LARGE($C$2:$C$937,3))+INDIRECT("E"&match(E954,$E$1:$E$940,0))<=5000,))
=IF(LARGE($C$2:$C$937,1)<5000,)
1st I think your logic is not correct. I would rather have Zanggose 4 as 1st, then Yanmega 3, then Zanggose 2 or Zangoose 3...
you can use a formula like:
=INDEX(B3:B8,MATCH( SMALL( IF($C$3:$C$8<5000,(5000-$C$3:$C$8)*100/$D$3:$D$8,100),*ROW($A$1:$A$6)* ), IF($C$3:$C$8<5000,(5000-$C$3:$C$8)*100/$D$3:$D$8,100),0),)
and insert the Function as Array (with ctrl+sht+enter) to as many cells as needed (paste formula in top cell, fix it, then enter as Array, then select all destination cells, press F2 to edit formula, then ectrl+sht+enter)
adjusting c3:c8 to your c928:c933 and so on, then ROW($A$1:$A$6) as big as you data.
Problem is that too many calls are made recursively and in large number of rows things will get very slow... You better use an extra Column with the formula
=IF($C$3:$C$8<5000,(5000-$C$3:$C$8)*100/$D$3:$D$8,100)
and then Filter/sort the data with VBA or Filters (there are excellent youtube videos on the subject, as long as company policies (if any) allow usage of VBA).

Exclude specific cells from being included in a formula's calculation, even if said cells are referenced by the formula

I maintain a spreadsheet for a virtual racing competition. Within it, I have to log the times of each racer and how many points they have earned. I do so by importing a .csv file for each race (the import takes place on a secondary document, which is linked to the main one via DGET if that matters).
Screenshot of a spreadsheet page
In Column G, I calculate the total time for all races, via basic addition: =C5+E5, =C6+E6 etc.
But sometimes racers crash, which automatically replaces their time (HH:MM:SS.MsMs) in the .csv file, with a percentage value (%) denoting how far they got when they crashed. This causes the addition formula to spit out a wildly inaccurate result, as the software does its best to add a time value and a percentage value which represent a distance together.
Can I write a formula which only adds together the cells which have the proper time format?
So, for example, I set X99, Y99, and Z99, to be added together, but if Y99 contains a percentage value, it is excluded from the calculation, and the final output is the value of X99+Z99
Something like this?
= IF(REGEXMATCH(TO_TEXT(X99),"%"),0,X99)
+ IF(REGEXMATCH(TO_TEXT(Y99),"%"),0,Y99)
+ IF(REGEXMATCH(TO_TEXT(Z99),"%"),0,Z99)
It turns the number into a text string, then searches for a "%" in the string; if it finds one it adds 0, otherwise adds the time value.
Documentation:
REGEXMATCH
IF

Google Sheets: Add value to total if a row condition matches for infinite column

I am attempting to make a Google Sheet that has a column for a rate and a value of that rate. Each night I am adding a new value to the bottom of this list so I want it to continue as an infinite list. What this sheet should do is look down the list of rates, if the rate matches a predefined rate, it should add the corresponding value to the total for that rate. I have four common rates that occur in no particular order and with some given value. I would like to total the values for each of those rates and give them in the totals section so each rate can be easily referenced.
Is there a way to do this using Google Sheets built-in commands? Pseudocode would be something like IF(A2:A = rate, add value in B to total). I know to make an infinite reading of a column by A2:A, but I don't know how to check each value as it goes through that column. I have attached a test spreadsheet with some sample values and the output that should be given by the formula. Thank you in advance for your help.
https://docs.google.com/spreadsheets/d/19k1DMipSsI9tWSjTXrVAPzPoaenWS6WYmlwe201_WQk/edit?usp=sharing
I managed to discover my own answer using the SUMIF statement. My code for the rate of .75 is ""=SUMIF(A2:A,"=.75",B2:B)"". I ran into some more trouble with the section for a different rate because I was having trouble saying not equal to .75 or 1 or 1.5 or 1.75. I solved this problem by completely bypassing it and adding the all the values and subtracting away the values already added. I would still like to know how to give the list of "ors" if someone could shed some light on this. But I now have a working solution regardless. I have updated the test sheet to show the solution.

Resources