give function random javascript
- User
give function random javascript
Assistant
Here's a JavaScript function that generates random numbers:
function random(min, max) { return Math.random() * (max - min) + min; } // Usage examples: console.log(random(1, 10)); // Random decimal between 1 and 10 console.log(random(0, 100)); // Random decimal between 0 and 100If you want random integers instead of decimals:
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } // Usage examples: console.log(randomInt(1, 10)); // Random integer between 1 and 10 (inclusive) console.log(randomInt(0, 100)); // Random integer between 0 and 100 (inclusive)You can also use
Math.random()directly:Math.random()returns a decimal between 0 (inclusive) and 1 (exclusive)Math.floor(Math.random() * 10)returns integers from 0 to 9Math.floor(Math.random() * 6) + 1returns integers from 1 to 6 (like a dice roll)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.