pseudo random generator in javascript

In JavaScript, you can use the built-in Math.random() function to generate pseudo-random numbers. The function returns a random float value between 0 (inclusive) and 1 (exclusive).

To generate a random integer between a and b (inclusive), you can use the following algorithm:

index.tsx
function getRandomInt(a, b) {
  return Math.floor(Math.random() * (b - a + 1)) + a;
}
86 chars
4 lines

Here's an example usage:

index.tsx
// Generates a random number between 1 and 10 (inclusive)
const randomNumber = getRandomInt(1, 10);
console.log(randomNumber);
127 chars
4 lines

You can also use other algorithms to generate pseudo-random numbers, such as the Linear Congruential Generator (LCG) or the Mersenne Twister algorithm. These algorithms provide better randomness and can be implemented using external libraries in JavaScript.

gistlibby LogSnag