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

Random always same in start

ANSWERED

How can I make Math.random() not always generate same numbers when starting an app in the simulator?

Best Answer
1 BEST ANSWER

Accepted Solutions

Yes

 

Got random to work by making a loop millisecond, of current time, number of times to generate different amount of random number each time the simulator starts, instead of always getting the identical range So did not need to write my own new random function.

 

Like:

 

let timer = new Date().getMilliseconds();
let total = 0
console.log("timer:" + timer);
for (; timer > 0; timer--) {
  total += Math.random();
}
console.log("timer:" + timer + ", total: " + total);

 

 

BTW I had to use the random generated number else the build would see it was not used and not do it.

Hence this did not work:

 

for (; timer > 0; timer--) {
  Math.random();
}

 

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Yes

 

Got random to work by making a loop millisecond, of current time, number of times to generate different amount of random number each time the simulator starts, instead of always getting the identical range So did not need to write my own new random function.

 

Like:

 

let timer = new Date().getMilliseconds();
let total = 0
console.log("timer:" + timer);
for (; timer > 0; timer--) {
  total += Math.random();
}
console.log("timer:" + timer + ", total: " + total);

 

 

BTW I had to use the random generated number else the build would see it was not used and not do it.

Hence this did not work:

 

for (; timer > 0; timer--) {
  Math.random();
}

 

Best Answer
0 Votes

Thanks for posting this. We have a ticket for the issue.

Best Answer
0 Votes