I am trying to create my own custom function in Google sheets based on my VBA code but its not working.
Function MyRandBetween(lowint As Long, highint As Long) As Long
Randomize
MyRandBetween = Round(lowint + Rnd() * (highint - lowint), 0)
End Function
You simply need to use Math.random().
Example in Google Sheets:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(MyRandBetween(1,100));
}
function MyRandBetween(low, high) {
return Math.floor(Math.random() * high) + low;
}
Script:
Result:
// Function MyRandBetween(lowint As Long, highint As Long) As Long // Randomize // MyRandBetween = Round(lowint + Rnd() * (highint - lowint), 0) // End Function const getRandom = (low, hi) => { return Math.round(low + Math.random() * (hi - low)); }; console.log(getRandom(1, 10)); console.log(getRandom(30, 100)); console.log(getRandom(20, 30));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.