36
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascriptで乱数を取得

Posted at

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

##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
36
19
0

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
  3. You can use dark theme
What you can do with signing up
36
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?