generate a random number between 100 and 999 in javascript

You can generate a random number between 100 and 999 in JavaScript using the Math.random() method, and some simple arithmetic. Here's an example:

index.tsx
// Generate a random number between 100 and 999
var min = 100;
var max = 999;
var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;

// Display the random number in the console
console.log(randomNumber);
220 chars
8 lines

In this code, we first define two variables, min and max, which set the range of possible numbers. Then, we use Math.random() to generate a random decimal between 0 and 1. We multiply this decimal by the range (i.e. max - min + 1) to get a number from 0 to max - min. We then add min to the result to shift the range up to start at min. Finally, we use Math.floor() to round the result down to the nearest integer, and assign it to the variable randomNumber.

If you run this code multiple times, you should see different random numbers between 100 and 999 printed to the console each time.

related categories

gistlibby LogSnag