Generate Random Number

JS
S
JavaScript

Simple guide to create random numbers in various programming languages. Covers basic syntax and common use cases for beginners.

1function getRandomInt(min, max) {
2  min = Math.ceil(min);
3  max = Math.floor(max);
4  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
5}
6
7// 50% chance
8const key: keyof FruitBasket = Math.random() > 0.5 ? 'apples': 'pears'; 
9const fruits = fruitBasket[key];

Created on 5/16/2018