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.tsx220 chars8 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.
gistlibby LogSnag