Generate random number with LESS

// Mixin to generate simple random number .random-number(@min, @max) { @random-number: floor(`Math.random() * @{max} + @{min}`); } /* Mixin to generate advanced 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(); @random-number: @base + @min; } // ############################### // Simple usage .random-integer { .random-number(1, 100); .makeRandom(); foo: @random-number; } // Usage for cache invalidation .random-cache-invalidation { .makeRandom(@max: 2); /* random decimal 0 - 2 */ .random-number(1000, 9999); background-image: src("your-image.jpg?v=@{random-number}"); } // Usage for make random color .random-color { .makeRandom(10, 20, 1); /* random integer 10 - 20 */ .random-number(0, 360); color: hsl(@random-number, 50%, 50%); }
Mixing for random number generation

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.