cancel
Showing results for 
Search instead for 
Did you mean: 

Random Range Int and Float

gausroth
Partner

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);
}

 

GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git
Discord: https://discord.gg/hNyDQwdw4N
YouTube: https://www.youtube.com/@GausRothTutorials
1 REPLY 1

gausroth
Partner

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));
}
GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git
Discord: https://discord.gg/hNyDQwdw4N
YouTube: https://www.youtube.com/@GausRothTutorials