JavaScript備忘録

ランダムな整数値を生成

Math.random()とMath.floor()の組み合わせ

  • 0,1,2のランダムな整数値
Math.floor(Math.random() * 3)
  • 0,…,nのランダムな整数値
Math.floor(Math.random() * (n + 1))
  • min,…,maxのランダムな整数値
Math.floor(Math.random() * (max + 1 - min)) + min
  • サイコロの実装
Math.floor(Math.random() * 6) + 1
PAGE TOP
タイトルとURLをコピーしました