function gaussian(mean, variance) {
return Math.sqrt(-2 * variance * Math.log(Math.random())) *
Math.cos(2 * Math.PI * Math.random()) + mean;
}
const n = 2;
const columns = 4*n;
const rows = 3*n;
const cellWidth = 640/columns;
const matrix = {};
let population = rows*columns;
const numParents = Math.ceil(0.2*rows*columns);
for (let i = 0; i < rows; i++) {
matrix[i] = {};
for (let j = 0; j < columns; j++) {
matrix[i][j] = 0;
}
}
let p = 0;
while (p < numParents) {
try {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
if (p < numParents) {
if (matrix[i][j] === 1) {
continue;
}
if (Math.abs(gaussian(0,columns) > j)) {
matrix[i][j] = 1;
++p;
}
} else {
throw "";
}
}
}
} catch (e) {}
}
const n = 2;
const columns = 4*n;
const rows = 3*n;
const cellWidth = 640/columns;
const matrix = {};
for (let i = 0; i < rows; i++) {
matrix[i] = {}
for (let j = 0; j < columns; j++) {
matrix[i][j] = 0
}
}
let population = rows*columns;
let numParents = Math.ceil(0.2*rows*columns);
for (let i = 0; i < numParents; i++) {
let success = false;
while (!success) {
let columnIndex = null;
while (columnIndex === null) {
const rand = Math.floor(Math.abs(gaussian(0, columns)));
console.log(rand);
if (rand < columns) {
columnIndex = rand
}
}
const rowIndex = Math.floor(Math.random()*rows);
if (matrix[rowIndex][columnIndex] === 0) {
matrix[rowIndex][columnIndex] = 1;
success = true;
}
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Algorithm 1 | |
Algorithm 2 |
Test name | Executions per second |
---|---|
Algorithm 1 | 19323.2 Ops/sec |
Algorithm 2 | 13451.1 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares two selection algorithms for generating random numbers.
Benchmark Definition JSON Analysis
The provided Benchmark Definition JSON defines two test cases, "Algorithm 1" and "Algorithm 2". Both tests use a simulated population of cells in a matrix, where each cell is initialized to 0. A parent cell is randomly selected, and with a probability of 20% (based on the Gaussian distribution), the cell is set to 1.
Options Compared
The two algorithms being compared are:
Math.random()
and selects a row index based on that value.Pros and Cons
Math.random()
.Library and Purpose
The Gaussian function used in both algorithms is a common mathematical formula for generating random numbers from a normal distribution. In this context, it's used to simulate the natural variability of real-world data.
Special JavaScript Feature/ Syntax
None mentioned in the provided Benchmark Definition JSON.
Other Considerations
When selecting a random number generator, it's essential to consider factors such as:
Alternatives
If you're looking for alternative JavaScript libraries or implementations, some popular options include:
Keep in mind that the choice of library or implementation ultimately depends on your specific use case and requirements.