Last active
September 24, 2025 09:28
-
-
Save floz/778026b2c77b580a19fa628331ad844f to your computer and use it in GitHub Desktop.
randByPower.js
This file contains hidden or 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
| function randByPower( min, max, power, rfc = randFc ) { | |
| if( min == max ) { | |
| return min | |
| } | |
| const value = rfc() * ( max - min ) + min | |
| return mapToPower( value, min, max, power ) | |
| } | |
| function sign( value ) { | |
| return value != 0 ? ( value < 0 ? -1 : 1 ) : 0; | |
| } | |
| function mapToPower( value, min, max, power ) { | |
| const valueSign = sign( value ) | |
| power = Math.abs( power ) | |
| value = ( value - min ) / ( max - min ) | |
| value = valueSign > 0 ? value : value - 1 | |
| value = Math.pow( value * valueSign, Math.abs( power ) ) | |
| value = valueSign > 0 ? value : 1 - value | |
| return value * ( max - min ) + min | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment