' Math.random() restituisce un numero casuale compreso tra 0 (incluso) e 1 (esclusivo):
https://www.w3schools.com/js/tryit.asp?filename=tryjs_math_random <!DOCTYPE html> <html> <body> <h2>JavaScript Math.random()</h2> <p>Math.random() returns a random number between 0 and 1:</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = Math.random(); </script> </body> </html> 'JavaScript Math Constants 'Math.E: 2.718281828459045 'Math.PI: 3.141592653589793 'Math.SQRT2: 1.4142135623730951 'Math.SQRT1_2: 0.7071067811865476 'Math.LN2: 0.6931471805599453 'Math.LN10: 2.302585092994046 'Math.LOG2E: 1.4426950408889e'4 'Math.Log10E: 0.4342944819032518 https://www.w3schools.com/js/tryit.asp?filename=tryjs_math_constants <!DOCTYPE html> <html> <body> <h2>JavaScript Math Constants</h2> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "<p><b>Math.E:</b> " + Math.E + "</p>" + "<p><b>Math.PI:</b> " + Math.PI + "</p>" + "<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" + "<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" + "<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" + "<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" + "<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" + "<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>"; </script> </body> </html> ' Oppure ' Math.floor(Math.random() * 11); // returns a random integer from 0 to 10 ' Math.floor(Math.random() * 100); // returns a random integer from 0 to 99 ' Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10 ' Ogni volta che fai clic sul pulsante, getRndInteger (min, max) restituisce un numero casuale compreso tra 0 e 9 (entrambi inclusi): https://www.w3schools.com/js/tryit.asp?filename=tryjs_random_function <!DOCTYPE html> <html> <body> <h2>JavaScript Math.random()</h2> <p>Every time you click the button, getRndInteger(min, max) returns a random number between 0 and 9 (both included):</p> <button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">Click Me</button> <p id="demo"></p> <script> function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min)) + min; } </script> </body> </html> |