Skip to curriculum
Jump to the editor

1.7. Random integer in a range, and a random hex color

Easy
Topics
10–20 min

Picking a random item, rolling a die in a game, choosing a placeholder avatar color: each one turns Math.random(), a fraction from 0 up to (but not including) 1, into a whole number in a range. The conversion is easy to get slightly wrong, and the result still looks random: the highest value never appears, or the two ends come up half as often as the others.

Write two functions in random.js: randomInt(min, max) and randomHexColor().

Signature

JAVASCRIPT
function randomInt(min, max) // an integer from min to max, both included
function randomHexColor()    // a string such as "#3fa0c2"

Requirements

  1. randomInt returns an integer from min to max, both included: min when Math.random() gives 0, max when it gives a number just below 1.
  2. Every integer in the range is equally likely: evenly spread random numbers give each value the same count.
  3. It works for negative ranges and for a range of one value (min === max).
  4. A bound with a fraction is moved inward: min is rounded up and max down, so randomInt(1.5, 4.5) gives 2, 3 or 4.
  5. randomInt calls Math.random exactly once.
  6. randomInt throws a RangeError when min is greater than max, when no integer lies between them (randomInt(1.2, 1.8)), or when a bound is NaN or infinite.
  7. randomHexColor multiplies Math.random() by 16,777,216, takes the floor, converts that integer to six lower-case hexadecimal digits (padded with zeros in front), and prefixes #. It calls Math.random exactly once.
  8. Different random values can map to the same color. The two ends are "#000000" (when Math.random() gives 0) and "#ffffff" (when it gives a number just below 1).

Examples

JAVASCRIPT
randomInt(1, 6);      // 1, 2, 3, 4, 5 or 6
randomInt(-3, 3);     // -3 to 3
randomInt(1.5, 4.5);  // 2, 3 or 4
randomInt(6, 1);      // throws RangeError
JAVASCRIPT
randomHexColor();     // "#3fa0c2", "#00a1ff"…
// Math.random() gives 0          → "#000000"
// Math.random() gives 0.5        → "#800000"
// Math.random() gives 0.99999999 → "#ffffff"

Environment

  • Plain JavaScript in a browser page; the tests call the globals randomInt and randomHexColor from random.js.
  • Most tests replace Math.random with a function that returns chosen numbers, so use Math.random (not crypto.getRandomValues).
  • Bounds are small enough to be exact: the tests use ranges of at most a hundred integers. Near Number.MAX_SAFE_INTEGER, numbers can no longer hold every integer, and Math.random has too few distinct values to reach every integer of a huge range evenly.

Hints

  1. How many integers are there from min to max, both included?
  2. What does (255).toString(16) return? And (5).toString(16)?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…