Created
March 27, 2024 18:12
-
-
Save tomhazledine/ba2ac1c046d0a86ccab09e9dde807871 to your computer and use it in GitHub Desktop.
Generate random-ish bell curve data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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