Last active
May 27, 2017 09:56
-
-
Save juanbrujo/91be803643a2b0007061 to your computer and use it in GitHub Desktop.
Mixin to generate random number with LESS
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
/* Mixin to generate random number | |
int should be 0 or 1, 1 being to make it an integer | |
*/ | |
.makeRandom(@min: 0, @max: @min+1, @int: 0) { | |
.checkInt() { | |
@getNum: `Math.random() * (@{max} - @{min} + @{int})`; | |
@base: unit(`@{int} == 1 ? Math.floor(@{getNum}) : @{getNum}`); | |
} | |
.checkInt(); | |
@randNum: @base + @min; | |
} | |
/* Use | |
*/ | |
.rand1 { | |
.makeRandom(); /* straight random decimal between 0 - 1 */ | |
random-number: @randNum; | |
} | |
.rand2 { | |
.makeRandom(@max: 2); /* random decimal 0 - 2 */ | |
random-number: @randNum; | |
} | |
.rand3 { | |
.makeRandom(10, 20, 1); /* random integer 10 - 20 */ | |
random-number: @randNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment