LoginSignup
1
1

More than 5 years have passed since last update.

[JavaScript] rand関数

Last updated at Posted at 2013-05-11

整数で返るrand関数をさくっと作っておきましょう。

var rand = function(n){
    return Math.floor(Math.random() * n) + 1;
};

こんなかんじで関数を定義して、下のように使います。

console.log(rand(100));
// -> 1~100のランダムな数が返る
1
1
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
1
1