-
Notifications
You must be signed in to change notification settings - Fork 3
/
55 Random().html
37 lines (30 loc) · 1.41 KB
/
55 Random().html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Random()</title>
</head>
<body>
<button onclick="document.getElementById('demo').innerHTML=getInteger(0,20)">Click Me</button>
<p id="demo"></p>
<script>
//Math.random() returns a random number between 0 (included) and 1 (excluded):
console.log(Math.random());
//Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both included):
console.log(Math.floor(Math.random()*10));
//can also use ceil in place of floor
//Math.floor(Math.random() * 11) returns a random integer between 0 and 10 (both included):
console.log(Math.floor(Math.random()*11));
console.log(Math.floor(Math.random()*100)); //returns random integer between 0 to 99
console.log(Math.floor(Math.random()*10)+1); //returns random integer between 1 to 10
console.log(Math.floor(Math.random()*100)+1); //returns random integer between 1 to 100
console.log(Math.floor(Math.random()*300)+100) //returns random integer between 100 to 300
//returns integer between min and max values
function getInteger(min,max){
return Math.floor(Math.random()*(max - min +1)) +min;
}
</script>
</body>
</html>