Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.random() vs. random()
(version: 0)
Comparing performance of:
Math.random() vs random()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
class EntropyPool { #entropy; #index constructor(poolSize = 1024) { this.#entropy = new Uint32Array(poolSize) this.#index = 0 crypto.getRandomValues(this.#entropy) } next() { const value = this.#entropy[this.#index++] if (this.#index === this.#entropy.length) { crypto.getRandomValues(this.#entropy) this.#index = 0 } return value } } const pool = new EntropyPool() function random() { return pool.next() / 4294967296 }
Tests:
Math.random()
Math.random()
random()
random()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.random()
random()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.random()
13834828.0 Ops/sec
random()
22663524.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition:** The benchmark definition provides two different JavaScript functions: `Math.random()` and `random()`. The `random()` function uses an EntropyPool class, which generates random numbers using the `crypto.getRandomValues` method. In contrast, `Math.random()` is a built-in JavaScript function that generates pseudo-random numbers. **Options Compared:** The benchmark compares two options: 1. **`Math.random()`**: This is a built-in JavaScript function that uses an Mersenne Twister algorithm to generate pseudo-random numbers. 2. **`random()`**: This function uses the EntropyPool class, which generates random numbers using the `crypto.getRandomValues` method. **Pros and Cons:** * **`Math.random()`**: + Pros: Fast and efficient, widely supported, and easy to use. + Cons: Pseudo-random numbers may not be suitable for cryptographic purposes or applications requiring high-quality randomness. The algorithm can also produce predictable patterns for certain inputs. * **`random()`**: + Pros: Generates cryptographically secure random numbers using the `crypto.getRandomValues` method, suitable for applications requiring high-quality randomness. + Cons: Slower than `Math.random()` due to the overhead of generating random values from the entropy pool. The EntropyPool class is also more complex and may require additional setup. **Library:** The `EntropyPool` library is used in the `random()` function to generate random numbers using the `crypto.getRandomValues` method. This library provides a simple way to access cryptographically secure pseudo-random numbers, which are suitable for applications requiring high-quality randomness, such as: * Cryptographic protocols * Random number generators * Statistical simulations **Special JS Feature or Syntax:** The benchmark uses the `class` syntax to define the EntropyPool class. This is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). It allows for more concise and readable code than traditional function-based classes. **Alternatives:** * For generating pseudo-random numbers, you can use other built-in functions like `Date.now()` or `WebCrypto API`. * For generating cryptographically secure random numbers, you can use the `crypto.getRandomValues` method directly without creating a custom EntropyPool class. * If you need to generate high-quality randomness for specific applications, consider using external libraries like `seedrandom` or `randlib`.
Related benchmarks:
repeated Math.random() vs crypto.getRandomValues()
crypto.getRandomValues vs Math.random()
Math.random() vs. random() v2
Generate random string
Comments
Confirm delete:
Do you really want to delete benchmark?