pythonで小数を含むランダムな範囲内の乱数を取得する方法/random.uniform関数の使い方

bead IT

random.uniform関数では指定した範囲内の乱数を取得することができます。ふたつの引数のうち大きいほうが最高値、小さいほうが最低値となります(小数点のデータ上の都合で、値によって最高値が出ないこともあります)。

スポンサーリンク

random.uniform関数

python random uniform1
uniform関数を使う前に、random組み込みモジュールをimportしておく必要があります。
#python
import random
print(random.uniform(1,5))
あとはrandom.uniformの引数に最低値と最高値を指定すれば、
python random uniform2
その間のランダムな小数を含む乱数が取得されます。
python random uniform3
なお、ふたつの引数は自動的に大きいほうが最高値、小さいほうが最低値となります。

関連記事

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