Home > Web Front-end > JS Tutorial > How Can I Generate Repeatable Random Numbers in JavaScript?

How Can I Generate Repeatable Random Numbers in JavaScript?

DDD
Release: 2024-12-11 09:31:09
Original
316 people have browsed it

How Can I Generate Repeatable Random Numbers in JavaScript?

Seeding the Random Number Generator in JavaScript

Introduction:

JavaScript's Math.random() function, while providing randomness, does not allow for seeding. This means that each time the function is invoked, it generates a new sequence of numbers, which can be problematic for situations where repeatable random sequences are desired.

Seedable Pseudorandom Number Generators:

Since Math.random() lacks seeding capabilities, it is necessary to implement external Pseudorandom Number Generators (PRNGs) that offer seeding functionality. PRNGs provide high-quality random numbers and allow for initialization with one or more seed values.

Seed Initialization:

PRNGs require proper initialization to ensure randomness. Well-distributed, high-entropy seed values are crucial for robust randomness. Hash functions, such as cyrb128, can generate suitable seeds from short strings. Alternatively, dummy data can be used as padding with multiple generator iterations to thoroughly mix the initial state.

Performance Considerations:

JavaScript numbers support only up to 53-bit resolution for whole integers and 32-bit resolution for bitwise operations. Modern PRNGs often use 64-bit operations, but shims are necessary for JS implementation, which can reduce performance drastically. The presented PRNG algorithms prioritize 32-bit operations for optimal performance in JavaScript.

Recommended PRNGs:

sfc32 (Simple Fast Counter)

sfc32 is a fast PRNG with a 128-bit internal state that excels in JavaScript. It exhibits excellent randomness quality and is commonly used in the PractRand random number testing suite.

Example Usage:

function sfc32(a, b, c, d) {
  return function() {
    // State update logic
    ...
    return (t >>> 0) / 4294967296;  // Convert to floating-point number in the range [0, 1)
  };
}

const seedgen = () => (Math.random() * 2 ** 32) >>> 0;
const getRand = sfc32(seedgen(), seedgen(), seedgen(), seedgen());
Copy after login

Note that the example uses a simple seed generation method for demonstration purposes. In practice, more robust seed generation mechanisms should be employed.

The above is the detailed content of How Can I Generate Repeatable Random Numbers in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template