LoginSignup
19

More than 5 years have passed since last update.

posted at

javascriptで乱数を取得

そんなに難しい話ではない。

3つのメソッド

前と後ろを含めるか否か。

// 0~100
var random = Math.round( Math.random()*100 );

// 1~100
var random = Math.ceil( Math.random()*100 );

// 0~99
var random = Math.floor( Math.random()*100 );

var random = Math.round( Math.random () * 20 ) + 3;
//3~23

var random = Math.ceil( Math.random () * 55 ) + 2;
//3~57

var random = Math.floor( Math.random () * 10);
//0~9

var random = Math.floor( Math.random () * 10) + 1;
//1~10

var random = Math.floor( Math.random () * 5) + 1;
//1~5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
19