【Python import random完全ガイド】乱数生成の基本から実践テクニックまで徹底解説
Pythonで乱数を扱う際に欠かせないrandomモジュールについて、基本的なimportの方法から実践的な活用テクニックまで、豊富なサンプルコードとともに詳しく解説します。ゲーム開発、データ分析、シミュレーションなど様々な場面で役立つ内容です。
📋 目次
🎯 randomモジュールとは
randomモジュールは、Pythonの標準ライブラリに含まれる乱数生成機能を提供するモジュールです。疑似乱数を生成し、ゲーム、シミュレーション、サンプリング、テストデータ作成など幅広い用途で使用されます。
特徴
- 標準ライブラリなので追加インストール不要
- 多様な乱数生成メソッドを提供
- 再現可能な疑似乱数
- 高速で実用的
🚀 importの基本パターン
1. 基本的なimport
import random
# 使用例
number = random.randint(1, 100)
print(number) # 1から100の整数
2. 特定関数のimport
from random import randint, choice
# モジュール名不要で使用可能
number = randint(1, 100)
item = choice(['apple', 'banana', 'orange'])
3. 全ての関数をimport
from random import *
# 全ての関数が直接使用可能
number = random()
integer = randint(1, 10)
4. エイリアスを使用
import random as rnd
# 短縮名で使用
number = rnd.randint(1, 100)
🎲 主要な乱数生成メソッド
1. random() – 基本的な浮動小数点乱数
import random
# 0.0以上1.0未満の浮動小数点数
value = random.random()
print(value) # 例: 0.8394
2. randint() – 整数乱数
# a以上b以下の整数
dice = random.randint(1, 6)
print(f"サイコロ: {dice}")
# 年齢の例
age = random.randint(18, 65)
print(f"年齢: {age}")
3. randrange() – 範囲指定整数
# start以上stop未満
number = random.randrange(0, 100) # 0-99
# ステップ指定
even_number = random.randrange(0, 100, 2) # 偶数のみ
4. uniform() – 範囲指定浮動小数点
# a以上b以下の浮動小数点数
temperature = random.uniform(20.0, 30.0)
print(f"気温: {temperature:.1f}°C")
5. choice() – リストから選択
colors = ['red', 'blue', 'green', 'yellow']
selected_color = random.choice(colors)
print(f"選択された色: {selected_color}")
# 文字列からも選択可能
char = random.choice('ABCDEFG')
print(f"選択された文字: {char}")
6. choices() – 重複ありの複数選択
# k個選択(重複あり)
numbers = [1, 2, 3, 4, 5]
selected = random.choices(numbers, k=3)
print(selected) # 例: [2, 2, 4]
# 重み付き選択
weights = [1, 1, 1, 5, 1] # 4が選ばれやすい
weighted_selection = random.choices(numbers, weights=weights, k=5)
print(weighted_selection)
7. sample() – 重複なしの複数選択
# 重複なしでk個選択
lottery_numbers = random.sample(range(1, 46), 6)
print(f"ロト6番号: {sorted(lottery_numbers)}")
# リストから重複なしで選択
items = ['A', 'B', 'C', 'D', 'E']
selection = random.sample(items, 3)
print(selection)
8. shuffle() – リストのシャッフル
# リストをその場でシャッフル
cards = ['A', 'K', 'Q', 'J', '10']
random.shuffle(cards)
print(cards) # 順序がランダムに変更される
9. gauss() – 正規分布乱数
# 平均値muと標準偏差sigmaの正規分布
height = random.gauss(170, 10) # 平均170cm、標準偏差10cm
print(f"身長: {height:.1f}cm")
💼 実践的な活用例
1. サイコロゲーム
import random
def roll_dice(num_dice=2):
results = [random.randint(1, 6) for _ in range(num_dice)]
total = sum(results)
print(f"サイコロ: {results}, 合計: {total}")
return total
# ゲーム実行
player_score = roll_dice()
computer_score = roll_dice()
winner = "プレイヤー" if player_score > computer_score else "コンピュータ"
print(f"勝者: {winner}")
2. パスワード生成
import random
import string
def generate_password(length=8):
characters = string.ascii_letters + string.digits + "!@#$%^&*"
password = ''.join(random.choices(characters, k=length))
return password
# 使用例
secure_password = generate_password(12)
print(f"生成されたパスワード: {secure_password}")
3. データサンプリング
# 大きなデータセットからサンプル抽出
large_dataset = list(range(1, 10001)) # 1万件のデータ
sample_data = random.sample(large_dataset, 100) # 100件サンプル
print(f"サンプル数: {len(sample_data)}")
print(f"平均値: {sum(sample_data) / len(sample_data):.1f}")
4. ランダムクイズ
quiz_questions = [
"Pythonの作者は?",
"Pythonの誕生年は?",
"Pythonの名前の由来は?"
]
def random_quiz():
question = random.choice(quiz_questions)
print(f"問題: {question}")
return question
# クイズ実行
selected_question = random_quiz()
5. シミュレーション(コイン投げ)
def coin_flip_simulation(num_flips=1000):
heads = 0
tails = 0
for _ in range(num_flips):
result = random.choice(['heads', 'tails'])
if result == 'heads':
heads += 1
else:
tails += 1
print(f"表: {heads}回, 裏: {tails}回")
print(f"表の確率: {heads/num_flips:.2%}")
coin_flip_simulation()
6. ランダムデータ生成
def generate_test_data(num_records=5):
names = ['田中', '佐藤', '鈴木', '高橋', '伊藤']
departments = ['営業', '開発', '人事', '経理']
employees = []
for i in range(num_records):
employee = {
'id': i + 1,
'name': random.choice(names),
'age': random.randint(22, 65),
'department': random.choice(departments),
'salary': random.randint(300, 800) * 10000
}
employees.append(employee)
return employees
# テストデータ生成
test_data = generate_test_data()
for emp in test_data:
print(emp)
🌱 シード値による制御
1. 基本的なシード設定
import random
# シード値を設定(再現可能な乱数)
random.seed(42)
print(random.randint(1, 100)) # 常に同じ値
# 異なる実行でも同じ結果
random.seed(42)
numbers = [random.randint(1, 10) for _ in range(5)]
print(numbers) # 常に同じ配列
2. 時間ベースのシード
import time
# 現在時刻をシードに使用
random.seed(int(time.time()))
truly_random = random.randint(1, 1000)
print(truly_random)
3. テスト用の固定シード
def test_random_function():
random.seed(123) # テスト用固定シード
result = some_random_calculation()
assert result == expected_value # 常に同じ結果
def some_random_calculation():
return sum(random.randint(1, 10) for _ in range(3))
⚡ パフォーマンスと注意点
1. 大量乱数生成の最適化
import random
# 効率的な方法
def generate_many_numbers_efficient(count=10000):
return [random.randint(1, 100) for _ in range(count)]
# 非効率な方法(避けるべき)
def generate_many_numbers_slow(count=10000):
numbers = []
for _ in range(count):
numbers.append(random.randint(1, 100))
return numbers
2. メモリ効率的なジェネレータ
def random_number_generator(start, end, count):
for _ in range(count):
yield random.randint(start, end)
# 使用例
for num in random_number_generator(1, 100, 5):
print(num)
3. 型チェックと例外処理
def safe_random_choice(items):
if not items:
return None
if not isinstance(items, (list, tuple, str)):
raise TypeError("引数はリスト、タプル、文字列である必要があります")
return random.choice(items)
# 使用例
result = safe_random_choice(['a', 'b', 'c'])
print(result)
🔒 セキュリティ関連の乱数
1. secretsモジュールとの比較
import random
import secrets
# 一般的な用途(ゲームなど)
game_number = random.randint(1, 100)
# セキュリティが重要な用途
secure_token = secrets.randbelow(1000000)
secure_string = secrets.token_hex(16)
print(f"ゲーム用: {game_number}")
print(f"セキュア: {secure_token}")
print(f"トークン: {secure_string}")
2. 使い分けの指針
# ❌ セキュリティ用途でrandomを使用(危険)
def weak_session_id():
return ''.join(random.choices('0123456789abcdef', k=32))
# ✅ セキュリティ用途でsecretsを使用
import secrets
def strong_session_id():
return secrets.token_hex(16)
🚨 よくあるエラーと解決法
1. TypeError: ‘module’ object is not callable
# ❌ エラーの原因
import random
number = random(1, 10) # エラー!
# ✅ 正しい書き方
import random
number = random.randint(1, 10)
2. ValueError: Sample larger than population
# ❌ エラーの原因
items = [1, 2, 3]
selection = random.sample(items, 5) # エラー!要素数より多い
# ✅ 正しい書き方
items = [1, 2, 3]
selection = random.sample(items, 2) # 要素数以下
3. 空のシーケンスでのchoice
# ❌ エラーの原因
empty_list = []
item = random.choice(empty_list) # エラー!
# ✅ 安全な書き方
def safe_choice(items, default=None):
return random.choice(items) if items else default
result = safe_choice([], "デフォルト値")
🎨 高度な応用テクニック
1. カスタム乱数クラス
class GameRandom:
def __init__(self, seed=None):
self.random = random.Random(seed)
def dice_roll(self, sides=6):
return self.random.randint(1, sides)
def coin_flip(self):
return self.random.choice(['表', '裏'])
def card_draw(self):
suits = ['♠', '♥', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
return f"{self.random.choice(ranks)}{self.random.choice(suits)}"
# 使用例
game = GameRandom(seed=42)
print(f"サイコロ: {game.dice_roll()}")
print(f"コイン: {game.coin_flip()}")
print(f"カード: {game.card_draw()}")
2. 重み付きランダム選択
def weighted_random_selection(items, weights):
if len(items) != len(weights):
raise ValueError("アイテムと重みの数が一致しません")
return random.choices(items, weights=weights, k=1)[0]
# 使用例
weapons = ['剣', '弓', '杖', '短剣']
rarity_weights = [50, 30, 15, 5] # 剣が最も出やすい
selected_weapon = weighted_random_selection(weapons, rarity_weights)
print(f"入手武器: {selected_weapon}")
3. ランダムイベントスケジューラ
import time
class RandomEventScheduler:
def __init__(self):
self.events = []
def add_event(self, name, probability):
self.events.append({'name': name, 'probability': probability})
def trigger_events(self):
triggered = []
for event in self.events:
if random.random() < event['probability']:
triggered.append(event['name'])
return triggered
# 使用例
scheduler = RandomEventScheduler()
scheduler.add_event('雨', 0.3)
scheduler.add_event('宝箱発見', 0.1)
scheduler.add_event('敵遭遇', 0.2)
events = scheduler.trigger_events()
print(f"発生イベント: {events}")
📊 実用的なパターン集
1. A/Bテスト用ランダム分割
def ab_test_assignment(user_id, test_ratio=0.5):
# ユーザーIDをシードにして一貫性を保つ
random.seed(hash(user_id))
return 'A' if random.random() < test_ratio else 'B'
# 使用例
users = ['user1', 'user2', 'user3', 'user4']
for user in users:
group = ab_test_assignment(user)
print(f"{user}: グループ{group}")
2. ランダムウォーク
def random_walk(steps=100):
position = 0
path = [position]
for _ in range(steps):
step = random.choice([-1, 1])
position += step
path.append(position)
return path
# 使用例
walk_path = random_walk(20)
print(f"最終位置: {walk_path[-1]}")
print(f"最大位置: {max(walk_path)}")
3. ランダムデータ検証
def validate_randomness(generator_func, num_tests=1000):
results = [generator_func() for _ in range(num_tests)]
unique_values = len(set(results))
average = sum(results) / len(results)
print(f"ユニーク値数: {unique_values}")
print(f"平均値: {average:.2f}")
print(f"最小値: {min(results)}")
print(f"最大値: {max(results)}")
# 使用例
validate_randomness(lambda: random.randint(1, 100))
🔍 よくある質問(FAQ)
Q: randomとnumpy.randomの違いは? A: randomは標準ライブラリで軽量、numpy.randomは科学計算に特化し多機能ですが、numpyのインストールが必要です。
Q: 真の乱数を生成できる? A: randomモジュールは疑似乱数です。真の乱数が必要な場合は、os.urandomやsecretsモジュールを使用してください。
Q: マルチスレッドで安全? A: randomモジュールは基本的にスレッドセーフですが、各スレッドで独立したRandomインスタンスを使用することを推奨します。
Q: パフォーマンスを向上させるには? A: 大量の乱数が必要な場合は、リスト内包表記を使用し、必要に応じてnumpy.randomの検討も有効です。
💡 ベストプラクティス
- 適切なメソッド選択: 用途に応じて最適な乱数生成メソッドを選ぶ
- シード管理: テスト時は固定シード、本番時は可変シードを使用
- セキュリティ考慮: 暗号化や認証には secrets モジュールを使用
- エラーハンドリング: 空のシーケンスや不正な範囲指定に対する対策
- パフォーマンス: 大量データ処理時は効率的な方法を選択
🎯 まとめ
Pythonのrandomモジュールは、乱数を扱う様々な場面で強力なツールとなります。基本的なimportから始まり、適切なメソッドの選択、実践的な活用法まで理解することで、ゲーム開発、データ分析、シミュレーションなど幅広い分野で活用できます。
重要なポイント:
- 標準ライブラリ – 追加インストール不要で即座に使用可能
- 豊富なメソッド – 用途に応じた多様な乱数生成機能
- 再現可能性 – シード値による制御で一貫した結果
- セキュリティ配慮 – 用途に応じてsecretsモジュールとの使い分け
randomモジュールを正しく理解し活用して、より魅力的で実用的なPythonプログラムを作成しましょう!
この記事は2025年7月時点の情報に基づいています。最新情報はPython公式ドキュメントをご確認ください。
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座