Skip to content

Instantly share code, notes, and snippets.

@floz
Last active September 24, 2025 09:28
Show Gist options
  • Select an option

  • Save floz/778026b2c77b580a19fa628331ad844f to your computer and use it in GitHub Desktop.

Select an option

Save floz/778026b2c77b580a19fa628331ad844f to your computer and use it in GitHub Desktop.
randByPower.js
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