Created
April 3, 2017 17:43
-
-
Save mitramejia/5eeff59a85c513382482e5de78d5b137 to your computer and use it in GitHub Desktop.
Get a random number between two numbers in scss
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
// 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