generate a random number between 1 and 100 in typescript

To generate a random number between 1 and 100 in TypeScript, you can use the Math.random() function. This function returns a random number between 0 and 1. You can multiply this number by 100 to get a random number between 0 and 100. Then, you can add 1 to the result to get a random number between 1 and 100.

Here's the TypeScript code:

index.ts
const randomNumber = Math.floor(Math.random() * 100) + 1;
console.log(randomNumber);
85 chars
3 lines

This code generates a random number between 1 and 100 and logs it to the console. The Math.floor() function rounds the number down to the nearest integer, which ensures that the result is an integer between 1 and 100.

related categories

gistlibby LogSnag