random number 1-100 in javascript

To generate a random number between 1 and 100 in JavaScript, you can use the Math.random() method, which returns a value between 0 (inclusive) and 1 (exclusive). You can then multiply that value by 100 to get a number between 0 and 100, and add 1 to get a number between 1 and 100. Here's the code:

index.tsx
const randomNumber = Math.floor(Math.random() * 100) + 1;
console.log(randomNumber); // prints a random number between 1 and 100
129 chars
3 lines

The Math.floor() method rounds the result down to the nearest integer to ensure that the result is between 1 and 100 and not a decimal value.

gistlibby LogSnag