mercredi 13 mai 2020

Random number between to values in google-sheets script not honoring max/min values

Just started learning app script/java and i want to get a random number between to values defined in 2 rows in a google sheet (see picture for clarification and debug info) I have the min/max value in two arrays for this purpose. max[x] and min[x]

So when i run this function from the script and outputs the values to my sheet it don't honor the max/min, (var pris = randomnumber(max,min)) but when i run the function from a cell with the same inputs it works as it should (=randomnumber(A1,A2)

The values in max/min are correct in the script as far as i can see in Logger and debug.

max, min have been declared as arrays earlier in the script and contains all the correct numbers, also pris.

So can anyone figure out what is wrong here? This is making me nuts!!! And i do need the script to write the numbers, not using the function from the cells.

Within a loop
        m = max[i];
        l = min[i];
        pris=randomnumber(m,l);
        price.push([pris]);
        i++;
 Within a loop   


function randomnumber(ma, mi) {

      // If i uncomment those two lines it works from the scrip, even if the value parameters are the same.
      // var mi = 80;
      // var ma = 120; 
    var randomNumber = Math.floor((Math.random() * (ma - mi)) + mi);

      return randomNumber;
    }

Screenshot




Aucun commentaire:

Enregistrer un commentaire