Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Last active May 27, 2017 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanbrujo/91be803643a2b0007061 to your computer and use it in GitHub Desktop.
Save juanbrujo/91be803643a2b0007061 to your computer and use it in GitHub Desktop.
Mixin to generate random number with LESS
/* 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