To generate a uniformly distributed random number in TypeScript, you can use the built-in Math.random()
method. This method returns a random floating-point number between 0 (inclusive) and 1 (exclusive) in a uniform distribution.
To generate a random number within a specific range, you can multiply the output of Math.random()
by the range and add the minimum value. For example, to generate a random number between 10 and 20 inclusive, you can do:
index.ts104 chars4 lines
The Math.floor()
function is used to round down the result to an integer value.
Note that the generated random number will always be a floating point number. If an integer number is required, the result can be rounded down or up depending on the desired rounding method.
gistlibby LogSnag