0
0

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 3 years have passed since last update.

ランダムな整数値を生成する

Posted at

ランダムな整数値を生成しよう。

0 1 2 3 4
0.00... 0.99... 1.99... 2.99... 3.99...

Math.random() 
//0.00...~0.99...1未満の数値をランダムで生成 

注意!1を含まない

###0から2までのランダムな整数を生成する。

Math.random * 3
//0.00...~2.99...までのランダムな整数を生成する。

整数値だけが欲しいので小数点以下を切ります。

Math.floor(Math.randomu() * 3)

###0からnと決まった範囲ではない場合

Math.floor(Math.random() * (n+1)

//応用 min ~ Max
min + Math.floor(Math.random() * (max + 1 - min))
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?