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.

JavaScript,7,乱数(ランダムな数値),Math.random,初心者向け

Last updated at Posted at 2020-12-15

ランダムな数字
JS(JavaScript)で乱数(ランダムな数値)を出す方法と、それを活用した実例を紹介しています。
配列と組み合わせいきます。

const randomNumber =Math.random();
console.log(randomNumber);

0〜1未満の数値をランダムに生成する

console.log(randomNumber * 5);
0~4.9まで

(randomNumber * 5 +1);
1~5.9まで

console.log(Math.floor(randomNumber *5 +1));
小数点切り捨て

document.getElementById("test").textContent = "今日のラッキーナンバーは" + 
Math.floor(randomNumber *5 +1);

今日のラッキーナンバーは○ここの数字がランダム

const omikuzi =  ["大吉", "中吉","凶"];
const omikuzirandomNumber = Math.floor(Math.random() * omikuzi.
length);
document.getElementById("omikuzi").textContent = omikuzi[omikuzirandomNumber];

配列とランダムの組み合わせでおみくじ

0
0
3

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?