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?

Python_randomライブラリで使用頻度が高い標準メソッド10選

Last updated at Posted at 2024-12-28

Python_randomライブラリで使用頻度が高い標準メソッド10選

  • 用途: 擬似乱数の生成、ランダムな選択や並べ替え。
  • 例: random.random, random.randint, random.choice
import random

使用率が高いメソッド10選

  1. random.random(): 0.0以上1.0未満の乱数を返します。

    print(random.random())
    
  2. random.randint(a, b): 指定した範囲 [a, b] 内の整数をランダムに返します。

    print(random.randint(1, 10))
    
  3. random.uniform(a, b): 指定した範囲 [a, b] 内の浮動小数点数をランダムに返します。

    print(random.uniform(1.0, 10.0))
    
  4. random.choice(seq): シーケンスからランダムに要素を1つ選択します。

    print(random.choice(["apple", "banana", "cherry"]))
    
  5. random.choices(population, weights=None, k=1): 指定した重みを持つ要素から重複を許可してk個選択します。

    print(random.choices(["red", "blue", "green"], weights=[10, 1, 1], k=2))
    
  6. random.sample(population, k): 指定した要素から重複せずにk個ランダムに選択します。

    print(random.sample(range(1, 50), 6))
    
  7. random.shuffle(x): リストの要素をランダムに並び替えます(インプレース操作)。

    numbers = [1, 2, 3, 4, 5]
    random.shuffle(numbers)
    print(numbers)
    
  8. random.seed(a=None): 乱数生成のシードを設定します(再現性のある結果を得るため)。

    random.seed(42)
    print(random.random())
    
  9. random.getrandbits(k): kビットのランダムな整数を返します。

    print(random.getrandbits(8))
    
  10. random.gauss(mu, sigma): 指定した平均(mu)と標準偏差(sigma)の正規分布に従う乱数を返します。

    print(random.gauss(0, 1))
    
    

+å 小数点以下の桁数を指定する方法

Pythonでは、数値の小数点以下の桁数を指定する方法がいくつかあります。以下に主要な方法をまとめます。


1. round() 関数を使う

round() 関数を使用して指定した桁数に丸めることができます。

value = 3.14159265

# 小数点以下2桁に丸める
result = round(value, 2)
print(result)  # 出力: 3.14

注意: round() は四捨五入の挙動をします。


2. フォーマット文字列 (f-string) を使う

表示用に桁数を揃えたい場合に便利です。

value = 3.14159265

# 小数点以下2桁まで表示
result = f"{value:.2f}"
print(result)  # 出力: 3.14

注意: この方法は文字列として扱われます。計算には使用できません。


3. format() メソッドを使う

フォーマット文字列と同様に、小数点以下の桁数を指定することができます。

value = 3.14159265

# 小数点以下2桁まで表示
result = "{:.2f}".format(value)
print(result)  # 出力: 3.14

4. Decimal モジュールを使う

高精度の計算や桁数の厳密な制御が必要な場合は、decimal.Decimal を使用します。

from decimal import Decimal, ROUND_HALF_UP

value = Decimal("3.14159265")

# 小数点以下2桁に丸める
result = value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(result)  # 出力: 3.14

ポイント: Decimal は金融計算や誤差にシビアな用途に適しています。


5. 数学的な方法 (10の累乗を使う)

数学的に小数点以下の桁数を直接制御する方法です。

value = 3.14159265

# 小数点以下2桁まで計算
factor = 10 ** 2
result = int(value * factor) / factor
print(result)  # 出力: 3.14

どの方法を選ぶべきか?

  • 計算目的: round()Decimal
  • 表示目的: f-string (f"{value:.2f}") や format()
  • 高精度計算: Decimal

用途に合わせて最適な方法を選んでください!

0
0
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
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?