基本問題(2)
[ edit ]
Contents
概要
[ edit ]
- 以下の関数を定義する:
- 与えられた整数の範囲から重複を許して乱数列を生成する関数
- random_sequence_with_repetition( length, min, max
- 与えられた整数の範囲から重複を許さずに乱数列を生成する関数
- random_sequence_non_overlapping( length, min, max )
- 返戻値は...
- 乱数列のリスト
- 引数は...
- 長さ: length
- 整数値範囲の下限値: min
- 整数値範囲の上限値(自身を含む): max
- 与えられた整数の範囲から重複を許して乱数列を生成する関数
ヒント
この課題で使うPythonの機能 (学習のヒント)
[ edit ]
この課題の解き方 (問題解決のヒント)
[ edit ]
- この課題の解き方 (問題解決のヒント)
- randomモジュールに便利なメソッドがあります
- randomモジュールのchoicesメソッド .... 重複を許す
- randomモジュールのsampleメソッド .... 重複を許さない
- rangeを使う場合,下限値+1を指定する必要があることに注意
- 別解としては...
- random.randint()やrandom.randrange()で,順次,乱数を生成し,リストに追加していけばよい
- 重複を許さない場合,新たに生成した乱数が,すでに生成した乱数列に含まれていれば,次の乱数を求めればよい
- randomモジュールに便利なメソッドがあります
>>> import random
>>> random.choices( range( 3, 10 ), k = 5 )
[8, 8, 6, 5, 9]
>>> random.choices( range( 3, 10 ), k = 5 )
[9, 5, 3, 3, 8]
>>> random.choices( range( 3, 10 ), k = 5 )
[4, 8, 7, 3, 7]
>>> random.choices( range( 3, 10 ), k = 5 )
[7, 8, 6, 5, 4]
>>> import random
>>> random.sample( range( 3, 10 ), k = 5 )
[8, 9, 5, 4, 6]
>>> random.sample( range( 3, 10 ), k = 5 )
[3, 6, 9, 7, 8]
>>> random.sample( range( 3, 10 ), k = 5 )
[6, 8, 5, 3, 4]
>>> random.sample( range( 3, 10 ), k = 5 )
[9, 4, 3, 6, 5]
- 実行時エラー
- randomモジュールのsampleメソッドは,長さが範囲よりも長いとき例外を送出する
>>> import random
>>> random.sample( range(1,3), k=10 )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "P:\0\Conda\lib\random.py", line 318, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
実行例
*
プログラム例: 本質的な部分 (授業中に順次公開します)
プログラム例: 配布コード (授業中に順次公開します)
[ edit ]
- 以下の解答例を配布します.
- 本質部分は替えませんが,コメントや,メインプログラム部分の実行例を,後々,分かり易く書き換える可能性はあります.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
# * Copyright (c) 2018 IIJIMA, Tadashi
# * (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
# ==============================================================================
# ソフトウェア工学[03] 基本課題[03]-(002a) BP_03_002a_random_sequence.py
# BP(Basic Problem) 03-002a: 乱数列のリスト
# 長さと上限/下限を指定する
# 重複を許すケースとないケースの二つの関数を作る
# 2018-10-17 飯島 正 (iijima@ae.keio.ac.jp)
# ==============================================================================
# ----- 乱数を取り扱ためのrandomモジュールをインポートする -----
import random
# ==============================================================================
# ===== 【関数定義】 与えられた整数の範囲から重複を許して乱数列を生成する関数 =====
# ※ 長さ: length, 整数値範囲の下限値: min, 整数値範囲の上限値(自身を含む): max
def random_sequence_with_repetition( length, min, max ):
return( random.choices( range( min, max + 1 ), k = length ) )
# ==============================================================================
# ===== 【関数定義】 与えられた整数の範囲から重複を許さずに乱数列を生成する関数 =====
# ※ 長さ: length, 整数値範囲の下限値: min, 整数値範囲の上限値(自身を含む): max
# ※ min~maxの範囲がlengthよりも短ければ,例外ValueErrorを送出します.
def random_sequence_non_overlapping( length, min, max ):
return( random.sample( range( min, max + 1 ), k = length ) )
# ==============================================================================
# ===== 【メイン・プログラム】 =====
# ----- オープニングメッセージ -----
print( "乱数列を生成します: " )
print( " ※重複を許す関数 ... random_sequence_with_repetition()" )
print( " ※重複を許さない関数 ... random_sequence_non_overlapping()" )
print()
# ----- パラメータの入力 -----
length = int( input( " 乱数の長さ \t>>> " ) )
min = int( input( " 下限値 \t>>> " ) )
max = int( input( " 上限値(自身を含む) \t>>> " ) )
# ----- 結果の表示 ----
print( "====== 重複を許す乱数列 =====" )
print( random_sequence_with_repetition( length, min, max ) )
print( random_sequence_with_repetition( length, min, max ) )
print( random_sequence_with_repetition( length, min, max ) )
print( random_sequence_with_repetition( length, min, max ) )
print( random_sequence_with_repetition( length, min, max ) )
print()
print( "====== 重複を許さない乱数列 =====" )
print( random_sequence_non_overlapping( length, min, max ) )
print( random_sequence_non_overlapping( length, min, max ) )
print( random_sequence_non_overlapping( length, min, max ) )
print( random_sequence_non_overlapping( length, min, max ) )
print( random_sequence_non_overlapping( length, min, max ) )
print()
# ==============================================================================