Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Random number generation in App

ANSWERED

I would like to generate a random integer between 0,5 for example on the App using something like:

let pick = Math.randomInt(0,5);

Is there a way to do that?  I get Type.error expected a function. I saw somewhere else on here that someone was able to use Math.Random()... I looked over the API but I'm not sure what has to be imported to get access to this function. Thanks. 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I've been using Math.random() with Math.floor() and I've not had to import anything extra.

 

var diceSize = 4;
function roll() {
  return Math.floor(Math.random() * Math.floor(diceSize)) + 1;
}

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Work around:  using getRandomValues on the companion then taking the output % 5 to get a random value between 0-5, and then file-transfering that to the App. Overly complicated but works.

Best Answer
0 Votes

I've been using Math.random() with Math.floor() and I've not had to import anything extra.

 

var diceSize = 4;
function roll() {
  return Math.floor(Math.random() * Math.floor(diceSize)) + 1;
}
Best Answer
0 Votes