get a uniformly distributed random number in javascript

To get a uniformly distributed random number in javascript, you can use the Math.random() function. This function generates a random number between 0 (inclusive) and 1 (exclusive), which means that it's not uniformly distributed. However, you can transform this number to be uniformly distributed between two values by multiplying it by the range of values and adding the minimum value. For example, if you want a random number between 1 and 10, you can use the following code:

index.tsx
function getRandom(min, max) {
  return Math.random() * (max - min) + min;
}

// Usage example:
const randomNumber = getRandom(1, 10);
console.log(randomNumber); // Outputs a random number between 1 and 10
206 chars
8 lines

The getRandom() function takes two arguments: the minimum and maximum values that you want to generate. It returns a random number that is uniformly distributed between these two values. By multiplying Math.random() by the range (max - min) and adding the minimum value (min), you ensure that the generated number is uniformly distributed.

gistlibby LogSnag