Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

【Javascript】乱数~ランダムな数字を出力する~

Posted at
0~1の間でランダムな数が出る

var randnum = Math.random(); 
console.log(randnum);

出力例
0.87420719877558
0.16926314395231623
0~10の間でランダムな数が出る
var randnum = Math.random()*10; 
console.log(randnum);

出力例
4.8754318842710065
8.9099030485857
0~10の間でランダムな整数が出る
var randnum = parseInt(Math.random()*10);
console.log(randnum);

出力例
3
9
1~3の間でランダムな数が出る
var randnum = parseInt(Math.random()*3) + 1; 
console.log(randnum);
5~7の間でランダムな数が出る
var randnum = parseInt(Math.random()*3) + 5; 
console.log(randnum);
0
0
1

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

Comments

tnbkyk689
@tnbkyk689

0~1の間でランダムな数が出る

1は含まれませんので、0以上1未満 が正解です。

0~10の間でランダムな数が出る
0~10の間でランダムな整数が出る

同様に、10になることはありませんので 0以上10未満 です。

0

Let's comment your feelings that are more than good

Being held Article posting campaign

0
0

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address