generate random float in typescript

To generate a random float in TypeScript, you can use the built-in Math.random() function. This function returns a random number between 0 (inclusive) and 1 (exclusive). To generate a random float in a specific range, you can multiply the result of Math.random() by the range and add the minimum value.

Here's an example function that generates a random float between a minimum and maximum value in TypeScript:

index.ts
function randomFloat(min: number, max: number): number {
  return Math.random() * (max - min) + min;
}
103 chars
4 lines

This function takes in two parameters: min and max, which represent the minimum and maximum value of the range you want to generate a random float in. The function uses the formula (Math.random() * (max - min)) + min to generate a random float in the specified range.

Here's an example usage of the randomFloat function:

index.ts
const myRandomFloat = randomFloat(10, 20);
console.log(myRandomFloat); // Output: a random float between 10 and 20
115 chars
3 lines

gistlibby LogSnag