GeneratorsLesson 58Free preview
Random Number Generator
This lesson is part of Web Developer Tools · All levels
Summary
A random number generator generates random numbers within a specified range. For ordinary needs, the built-in random functions of a programming language are sufficient, but for security such as tokens or passwords use a cryptographic generator. See if the upper and lower boundaries are inclusive so that the results are as expected.
Key points
- Math.random() yield decimal from 0 to less than 1
- The inclusive integer formula uses Math.floor and the range length plus one
- Use crypto.getRandomValues() for security purposes
- Generators with the same seed produce repeatable sequences
- Always check the bottom and top boundaries so as not to off-by-one
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.