math.random($limit: null) returns a random number when Sass compiles. With no argument, you get a decimal between 0 and 1. With a $limit ≥ 1, you get a whole number from 1 to $limit (inclusive). Load it with @use "sass:math" (Dart Sass 1.23+ for modules).
Official sample shapes.
math.random() → e.g. 0.2821251858 · math.random(10) → e.g. 4 · math.random(10000) → e.g. 5373 (exact values change each compile).
Compile-time only.
The CSS file stores a fixed number. Recompile to get new values. For live randomness in the browser, use JavaScript Math.random().
Units on $limit are deprecated.
Official docs: random currently ignores units on $limit (so 100px acts like 100). Future Dart Sass will keep those units. Prefer math.random(math.div($limit, 1px)) * 1px. Global random() is also deprecated in modern Dart Sass.
Syntax
Syntax
SCSS
@use "sass:math";
math.random($limit: null); //=> number
math.random(); // decimal in [0, 1)
math.random(10); // integer in 1…10
math.random(10000); // integer in 1…10000
// Future-safe unitful limit (recommended)
math.random(math.div(100px, 1px)) * 1px;
// Global name (legacy / deprecated in modern Dart Sass)
random(10);
// Module API — prefer math.random after @use "sass:math"
Pattern: choose decimal or integer mode → call once per needed value → remember the result is frozen in the compiled CSS.
Reference
Modes and Limits
Two call shapes:
Call
Result shape
math.random()
Unitless decimal, typically in [0, 1)
math.random(10)
Whole number from 1 to 10
math.random(10000)
Whole number from 1 to 10000
math.random(100px) (current)
Unitless integer 1…100 + deprecation warning
math.random(math.div(100px, 1px)) * 1px
Future-safe integer length in px
Sample CSS below varies.
View Compiled CSS panels show one verified compile. Your numbers will differ on the next run.
Examples target Dart Sass with @use "sass:math". Open View Compiled CSS for a sample compile (numbers change each run; aligned with official Sass docs).
Prefer math.random over deprecated global random().
One line: when CSS needs a compile-time random token, call math.random() or math.random($limit).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:math"
2. math.random() for 0…1; math.random($n) for 1…$n
3. Store the result if several rules must share one roll
4. For unitful limits: random(div($limit, 1u)) * 1u
5. Prefer math.random over global random()
Frequently Asked Questions
With no argument, math.random() returns a random decimal between 0 and 1. With math.random($limit) where $limit is a number ≥ 1, it returns a random whole number between 1 and $limit inclusive.
Add @use "sass:math"; then call math.random() or math.random(10). Prefer the module form in new SCSS. The global alias is random(), which is deprecated in modern Dart Sass.
Yes, each call can produce a new value when Sass compiles. Recompiling the same file usually changes the CSS numbers. It is not a browser runtime random function.
Official docs say random currently ignores units on $limit (so 100px behaves like 100). That unit-ignoring behavior is deprecated; a future release will return a random integer that keeps the same units as $limit. Prefer math.random(math.div($limit, 1px)) * 1px.
No. Sass random is fixed when CSS is generated. For values that change while the page runs, use JavaScript Math.random() or CSS features that resolve in the browser.
Built-in modules with @use need Dart Sass (about 1.23+). Older setups can still call the global random() name. Prefer math.random() for new code.