Tsallaka zuwa abun ciki
Justice Tech School

Random Numbers in Practice

Math.random gives a number from 0 up to but not including 1. Scale and floor it for integer ranges. It is not secure, use crypto.getRandomValues for secrets.

Example
function randInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
const r = randInt(1, 6);
console.log(r >= 1 && r <= 6);
const arr = new Uint32Array(1);
crypto.getRandomValues(arr);
console.log(typeof arr[0]);

Exercise

Secure random source.

crypto.(arr)

Quiz Test

Is Math.random safe for passwords?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.