Skip to content

Programming-0101/Math-Random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grokking Math.random()

Just a little project to render the significant digits of JavaScript's Math.random() in a table. I wanted to compare how the digits "repeat" in subsequent random numbers.

Quick FAQ

  • What is Math.random()?
    • Math.random() returns a random number between 0 and 1.
  • How many digits does Math.random() return?
    • Math.random() returns a number with 18 digits.
  • Why is Math.random() so random?
    • Math.random() is a pseudo-random number generator.
  • What is a pseudo-random number generator?
    • A pseudo-random number generator is a function that produces a sequence of numbers that appear to be random, but are not truly random.

Copilot Commentary

This response is based on the prompt I'm trying to understand what factors affect the randomness of numbers generated by JavaScript's Math.random() function. Can you help? given to Copilot.Microsoft.com.

Absolutely, I'd be happy to help!

The Math.random() function in JavaScript is a pseudorandom number generator, which means it generates numbers that appear to be random but are actually deterministic.

Here are some factors that affect the randomness of numbers generated by Math.random():

  1. Algorithm: The algorithm used by Math.random() is implementation-dependent, which means it can vary between JavaScript engines (like V8 for Chrome or SpiderMonkey for Firefox). These algorithms typically use an initial seed value to start the generation process.

  2. Seed: The seed is the starting point for the sequence of pseudorandom numbers. If you start with the same seed, you will get the same sequence of numbers. In most JavaScript engines, the seed is set when the engine starts, often to the current time in milliseconds.

  3. State: Once the generator starts producing numbers, the current "state" of the generator affects the next number. Each number generated updates the state, so the sequence of numbers is dependent on the sequence of states.

  4. Period: The period of a pseudorandom number generator is the length of the sequence before it starts repeating. For a good generator, this period should be very long.

  5. Uniformity: Ideally, a random number generator should produce numbers that are uniformly distributed across the range. That is, each number has an equal chance of being chosen.

Remember, while the numbers generated by Math.random() may seem random for many purposes, they are not suitable for tasks that require true randomness, such as serious cryptography or complex simulations.

Here's a simple example of using Math.random():

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

console.log(getRandomArbitrary(1, 100)); // Outputs a random number between 1 and 100

In this example, Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive), which is then scaled to the desired range (1 to 100 in this case). The result is a pseudorandom number between 1 and 100.

About

Just a little project to render the significant digits of Math.random() in a table.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published