Skip to content

Instantly share code, notes, and snippets.

@mitramejia
Created April 3, 2017 17:43
Show Gist options
  • Save mitramejia/5eeff59a85c513382482e5de78d5b137 to your computer and use it in GitHub Desktop.
Save mitramejia/5eeff59a85c513382482e5de78d5b137 to your computer and use it in GitHub Desktop.
Get a random number between two numbers in scss
// Given two numbers return the biggest one
@function get-biggest-num-between($a, $b) {
$c: null;
@if ($a > $b) {
$c : $a;
}
@if ($a < $b) {
$c : $b;
}
@else if ($a = $b){
@error 'Numbers must be different. #{$a} and #{$b} were given'
}
@return $c;
}
// Given two numbers return the smallest one
@function get-smallest-num-between($a, $b) {
$c: null;
@if ($a < $b) {
$c : $a;
}
@if ($a > $b) {
$c : $b;
}
@else if ($a == $b){
@error 'Numbers must be different. #{$a} and #{$b} were given'
}
@return $c;
}
// Description: Get a random number between two numbers
//
/// @throw Error if params are equal numbers
/// @throw Error if resulting value is not a number
// @example
// Given 4 and 8
// @return 5, 6, 7 or 8
@function random-num-between($a, $b) {
$biggest: get-biggest-num-between($a, $b);
$smallest: get-smallest-num-between($a, $b);
$needle: null;
$found: false;
@while $found == false {
$scale: random($biggest);
@if($scale > $smallest){
$found: true;
$needle: $scale;
}
}
@if(type-of($needle) != number){
@error 'Function returned a non number value: #{$needle} is #{type-of($needle)}'
}
@return $needle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment