I am using Google Sheets and Google Apps Script to try and calculate some basic probabilities. Replica sheet. The “gain” sheet shows the probability of gaining n points. On the “calc” sheet, I’d like to calculate the probability of each of the four players to be in the top two after gaining points (following the probability distribution given on the first sheet) but my code is inefficient and is exceeding the maximum execution time.
Here is the code I am using in my Apps Script to try and calculate the probabilities.
function fourcalc() {
var sheet=SpreadsheetApp.getActiveSheet();
//Read existing points as input in the calc sheet
var points1=sheet.getRange("C1").getValue();
var points2=sheet.getRange("C2").getValue();
var points3=sheet.getRange("C3").getValue();
var points4=sheet.getRange("C4").getValue();
var gain = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('gain');
var prob1=0;
var prob2=0;
var prob3=0;
var prob4=0;
//For loops to iterate over the table for each player
for(x=0;x<19;x++){
for(y=0;y<19;y++){
for(z=0;z<19;z++){
for(w=0;w<19;w++){
// Finding out the points gained in each instance
var v1=s1.getRange(2+x,1).getValue();
var v2=s2.getRange(2+y,1).getValue();
var v3=s3.getRange(2+z,1).getValue();
var v4=s3.getRange(2+w,1).getValue();
//Final points in each instance
var p=points1+v1;
var q=points2+v2;
var r=points3+v3;
var s=points4+v4;
//Calculating probabilities for each player winning at the end
if (p > q && p > r && p > s) {
var p1=s1.getRange(2+x,2).getValue();
var p2=s2.getRange(2+y,2).getValue();
var p3=s3.getRange(2+z,2).getValue();
var p4=s4.getRange(2+w,2).getValue();
var tprob=p1*p2*p3*p4;
prob1+=tprob;
}
if (q > p && q > r && q > s) {
var p1=s1.getRange(2+x,2).getValue();
var p2=s2.getRange(2+y,2).getValue();
var p3=s3.getRange(2+z,2).getValue();
var p4=s4.getRange(2+w,2).getValue();
var tprob=p1*p2*p3*p4;
prob2+=tprob;
}
if (r > p && r > q && r > s) {
var p1=s1.getRange(2+x,2).getValue();
var p2=s2.getRange(2+y,2).getValue();
var p3=s3.getRange(2+z,2).getValue();
var p4=s4.getRange(2+w,2).getValue();
var tprob=p1*p2*p3*p4;
prob3+=tprob;
}
if (s > p && s > q && s > r) {
var p1=s1.getRange(2+x,2).getValue();
var p2=s2.getRange(2+y,2).getValue();
var p3=s3.getRange(2+z,2).getValue();
var p4=s4.getRange(2+w,2).getValue();
var tprob=p1*p2*p3*p4;
prob4+=tprob;
}
}
}
}
}
sheet.getRange("C6").setValue(prob1);
sheet.getRange("C7").setValue(prob2);
sheet.getRange("C8").setValue(prob3);
sheet.getRange("C9").setValue(prob4);
}
I used a similar function to find out probabilities in case of two and three players (using two and three for loops respectively) and was able to get answers. The actual code is slightly different where the points gain tables are a bit bigger (30+ rows instead of 19) and sometimes modifying the if statements to calculate probabilities if the player is in top 2 out of 4 instead of just leading the whole pack by using if ((p > q && p > r) || (p > q && p > s) || (p > r && p > s))
but I don’t think those are the problems as opposed to the four nested for loops. Is there a more efficient way to go about this so that I can calculate the probabilities. I am new to coding and Apps Script so any guidance would be appreciated.
StrudelCutie4427 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.