LoginSignup
0
0

More than 5 years have passed since last update.

【JavaScript 】乱数を取得する

Posted at

乱数を取得する

'use strict';

// 0以上1未満の乱数
console.log(Math.random()); // 0 ~ 0.999...

// 1から6までのランダムな整数(サイコロ)
console.log(Math.floor(Math.random() * 6 + 1)); // 1 ~ 6

// min 以上 max 未満のランダムな整数
const min = 1;
const max = 10;
const num = Math.floor(Math.random() * (max - min) + min);
console.log(num); // 1 ~ 9

※Google Chrome で確認
※ES8

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