Skip to main content

πŸ“„ Random Number

Question Description​

Implement a random() that takes a min and max and returns a random number between min and max.

The function should return a random integer between the min and max values.

TypeScript Version​

function createRandomNumber(min: number, max: number): number {
if (min >= max) {
throw new Error('ε‚³ε…₯ηš„ min εƒζ•ΈδΈε―ε€§ζ–Όζˆ–η­‰ζ–Ό max');
}

return Math.floor(Math.random() * (max - min) + min);
}

console.log(createRandomNumber(0, 200)); // 0 ~ 199