Files
30-seconds-of-code/snippets/randomIntWithLengthInRange.md
2018-01-15 12:32:30 +03:00

582 B

randomIntWithLengthInRange

Returns a random integer in the specified range and length.

Use Array.from() to create an empty array of the specific length. Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

const randomIntWithLengthInRange = (len, min, max) =>
	Array.from({ length: len }, () =>
		Math.floor(Math.random() * (max - min) + min)
	);
Examples
randomIntWithLengthInRange(10, 12, 35); // [ 14, 20, 12, 29, 23, 16, 26, 22, 15, 24 ]