Sass math.random()

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

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

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.

Modes and Limits

Two call shapes:

CallResult 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)) * 1pxFuture-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.

SCSS Examples

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).

Example 1 — Official Docs Shape (No Argument)

A random decimal between 0 and 1.

SCSS
@use "sass:math";

@debug math.random(); // e.g. 0.2821251858

.basic {
  --a: #{math.random()};
}

How It Works

  1. No $limit → decimal mode.
  2. Result is unitless in roughly [0, 1).
  3. Useful for opacity or scale factors.
  4. Matches the official docs call shape.

Example 2 — Official Docs Shape (Integer Limit)

A random whole number from 1 to 10.

SCSS
@use "sass:math";

@debug math.random(10); // e.g. 4

.limit {
  --b: #{math.random(10)};
}

How It Works

  1. $limit is 10 (≥ 1).
  2. Result is an integer from 1 to 10.
  3. Inclusive of both ends.
  4. Matches the official docs call shape.

Example 3 — Official Docs Shape (Large Limit)

Same integer mode with a bigger ceiling.

SCSS
@use "sass:math";

@debug math.random(10000); // e.g. 5373

.large {
  --c: #{math.random(10000)};
}

How It Works

  1. Limit is 10000.
  2. Result is an integer from 1 to 10000.
  3. Still compile-time — frozen in CSS.
  4. Matches the official docs call shape.

Example 4 — Future-Safe Unitful Limit

Avoid the deprecated “ignore units” path.

SCSS
@use "sass:math";

$limit: 100px;

.units {
  // Recommended (keeps px on the result)
  --safe: #{math.random(math.div($limit, 1px)) * 1px};

  // Avoid: math.random(100px) — currently ignores units + warns
}

How It Works

  1. Strip the unit with math.div($limit, 1px).
  2. Randomize the unitless integer.
  3. Multiply by 1px to reattach the unit.
  4. Matches Dart Sass’s recommended migration.

Example 5 — Opacity and Branching

Use a random integer once, then branch with @if.

SCSS
@use "sass:math";

$n: math.random(4);

.box {
  @if $n == 1 { width: 10px; }
  @else if $n == 2 { width: 20px; }
  @else if $n == 3 { width: 30px; }
  @else { width: 40px; }

  opacity: math.random();
}

How It Works

  1. Store $n once so the branch uses one roll.
  2. Width picks one of four fixed sizes.
  3. Opacity uses a separate decimal roll.
  4. Output is static CSS until you recompile.

Edge Cases & Pitfalls

Most random bugs are expecting browser-time change, or passing units into $limit.

stage

Compile time, not runtime

The CSS number is fixed until you rebuild. Not for animations or user-triggered variety.

units

Do not pass units to $limit

Use math.random(math.div($limit, 1px)) * 1px instead of math.random(100px).

store

Call once, reuse the variable

Each math.random() call is a new roll. Store in $n when branches must agree.

global

Global random() is deprecated

Prefer math.random after @use "sass:math".

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.random()Dart Sass 1.23+ via @use "sass:math"
Global random()Works; deprecated in modern Dart Sass
Units on $limitCurrently ignored (deprecated); will keep units later
TimingResolved at compile time

Compile with the Dart Sass CLI.

Key Takeaways

  • No arg → decimal 0…1; limit → integer 1…limit.
  • Compile-time — CSS stores a fixed number.
  • Strip units first for future-safe length limits.
  • Prefer math.random over deprecated global random().

One line: when CSS needs a compile-time random token, call math.random() or math.random($limit).

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.

Next: Sass math.round()

Round a number to the nearest whole value.

math.round() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful