I'm using this code to get a random number in the range of min-max. for some reason, I get numbers that are bigger than max. (the console.log shows that the min and max are set correctly.) (I'm showing here the relevant code. this is not the whole file.)
const RandomNumber = () => {
const [min, setMin] = useState(0)
const [max, setMax] = useState(0)
const [rand, setRand] = useState(0)
const classes = useStyles();
const setRandom = () => {
console.log(min);
console.log(max);
let random = (Math.floor(Math.random() * (max - min + 1)) + min)
console.log(random);
setRand(random)
}
return (
<>
<Stack direction="row" spacing={2} className={classes.root}>
<TextField
id="filled-number"
label="Min-Number"
type="number"
InputLabelProps={{
shrink: true,
}}
variant="filled"
onChange={(e) => setMin(e.target.value)}
/>
<TextField
id="filled-number"
label="Max-Number"
type="number"
InputLabelProps={{
shrink: true,
}}
variant="filled"
onChange={(e) => setMax(e.target.value)}
/>
<Stack spacing={2} direction="row">
<Button variant="outlined" className={classes.button} onClick={setRandom}>Get Random!</Button>
</Stack>
</Stack>
<h1>rand is: {rand}</h1>
</>
);
}
export default RandomNumber;