Forum Discussion

gausroth's avatar
gausroth
Mentor
9 months ago

Random Range Int and Float

For those that are used to Unity might recognize this.

// returns a random whole number between min (inclusive) and max (exclusive)
randomRange(min: number, max: number) {
     return Math.floor(min + Math.random() * (max - min));
}

// returns a float between min (inclusive) and max (exclusive)
randomRange(min: number, max: number) {
     return min + Math.random() * (max - min);
}

 

1 Reply

  • Here is an updated version of the helper function

    RandomRange(min: number, max: number, isFloat: boolean) {
         if (isFloat) return min + Math.random() * (max - min);
         else return Math.floor(min + Math.random() * (max - min));
    }