Javascript/Math.random()

解説 ランダム値の取得
Math.random()

0~1のランダム数

---

function sample1Func(){
  var x = 0;
  x = Math.random();
  document.getElementById("sample1p").innerHTML = x;
}
	
Math.random()*10

0~10のランダム数

---

function sample2Func(){
  document.getElementById("sample2p").innerHTML = Math.random()*10;
}
	
Math.floor(Math.random()*10)

0~9のランダム整数

---

function sample3Func(){
  var y = Math.random()*10;
  y = Math.floor(y);
  document.getElementById("sample3p").innerHTML = y;
}
	
配列[Math.floor(Math.random()*配列.length)]

配列をランダム表示

---

function sample4Func(){
  var wood = ["アカマツ", "ツツジ", "ツバキ", "イチョウ", "サザンカ"];
  var wood = wood [ Math.floor(Math.random() * wood.length)];
  document.getElementById("sample4p").innerHTML = wood;
}
	
sample5
sample5code
var s5Img = document.getElementById("s5Img");
s5Img.style.width = "300px";
s5Img.style.height = "200px";
var s5Array = ["../../img/jura.jpg", "../../img/reims.jpg", "../../img/code.jpg"];
function s5Func(){
  var s5Rnd = Math.floor(Math.random()*s5Array.length);
  s5Img.src = s5Array[s5Rnd];
}
s5Func();