5
4

More than 3 years have passed since last update.

[Python]randrange()とrandint()の違い

Last updated at Posted at 2020-08-28

環境

・Python 3.8.5
・osx10.15.6

はじめに

Pythonで初めてランダムな数を用いるとき同じような表記があったので違いを記した。

randrange()とは

random.randrange(a, b)は『a』から『b-1』までの整数を返す。
引数が1つの場合は『0』から引数マイナス1の整数を返す。

random.randint()とは

random.randint(a, b)は『a』から『b』までの整数を返す。

違いは

最後の数を含むか含まないかの違い!
random.randrange(a, b)の方は最後の値を含まないのでlen(配列)と組み合わせて
配列の繰り返し処理の便利です!

参考

random関数について
https://www.lifewithpython.com/2013/04/random.html

5
4
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
5
4