pythonでランダムな整数を生成する方法/random.randint関数の使い方

dice IT

random.randint関数では、指定した数内のランダムな整数を返すことができます。

スポンサーリンク

random.randint関数

randint関数を使用するには、先に組み込みモジュールrandomimportする必要があります。
#python
import random
r = random.randint(0,10)
print(r)
randomをimportしたら、random.randintの後に生成したい範囲の最低値・最高値を引数として指定することで、
その範囲内の整数(この場合0~10)をランダムに返させることができます。

関連記事

関連記事

Python操作方法、関数・メソッドリファレンス
使用用途別にまとめたPythonリファレンスページです。 初期準備、基本概念 ダウンロード、インストール、起動 ShellとEditor、保存、実行 保存したPythonの起動 コメント、docstring、行またぎ コメント、...

コメント

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

モバイルバージョンを終了