Skip to content

Instantly share code, notes, and snippets.

@tomhazledine
Created March 27, 2024 18:12
Show Gist options
  • Save tomhazledine/ba2ac1c046d0a86ccab09e9dde807871 to your computer and use it in GitHub Desktop.
Save tomhazledine/ba2ac1c046d0a86ccab09e9dde807871 to your computer and use it in GitHub Desktop.
Generate random-ish bell curve data
export const generateRandomBellCurveData = (size, min = 0, max = 100) => {
// Function to add pronounced randomness to the curve
const randomize = (val, min, max, noiseLevel = 0.5) => {
// Apply random noise based on noise level
const noise = (Math.random() * 2 - 1) * noiseLevel;
return Math.max(min, Math.min(max, val + noise));
};
// Generate Y values with normal distribution characteristics
const yValues = Array.from({ length: size }, (_, i) => {
// Convert index to range from -1 to 1 (centered and normalized)
const x = (i / (size - 1)) * 2 - 1;
// Generate values based on a bell curve formula
const y = Math.exp(-Math.pow(x * 3, 2));
// Scale to the min-max range
const scaledY = min + y * (max - min);
// Add random fluctuations with a higher noise level for more pronounced randomness
return randomize(scaledY, min, max, 10); // Increase the third parameter for more noise
});
return yValues;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment