Home > Python > Python: モジュールにテスト関数を定義 - 重複のない乱数(整数MIN以上MAX以下)の生成

Python: モジュールにテスト関数を定義 - 重複のない乱数(整数MIN以上MAX以下)の生成

Sponsored Link

ソースコード

#!/usr/bin/python
# coding: UTF-8

import random

def make_randint_list(min, max, cnt, sortflag=False, revflag=False):
    """
    重複のない乱数(整数min以上max以下)を要素としたリストを返す
    """
    list = []
    i = 0
    while cnt != i:
        r = random.randint(min, max)
        try:
            list.index(r)   # 既にリストに存在するか
        except ValueError, e:
            list.append(r)  # 無い場合はリストに格納
            i = i + 1
    if (sortflag): list.sort(reverse=revflag)
    return list

def _main():
    """モジュールのチェック関数"""

    print 'makerand0.py [_main()]'
    print make_randint_list(10, 99, 10)
    print make_randint_list(10, 99, 10, True)       # 昇順ソート
    print make_randint_list(10, 99, 10, True, True) # 降順ソート

if __name__ == '__main__' : _main()

モジュール変数__name__の中には通常はモジュール名が入っています。しかし、このモジュールファイルを直接実行した場合は__name__に'__main__'という名前が入ります。その為、if __name__ == '__main__' : は真となり_main()が実行されます。

実行結果

$ python makerand0.py
makerand0.py [_main()]
[62, 18, 55, 44, 97, 67, 87, 16, 59, 43]
[11, 13, 30, 42, 46, 52, 71, 75, 81, 92]
[88, 81, 65, 60, 57, 53, 41, 34, 27, 23]

モジュールテスト用スクリプト

makerand0_test.py

#!/usr/bin/python
# coding: UTF-8

from makerand import make_randint_list

print make_randint_list(10, 99, 10)
print make_randint_list(10, 99, 10, True)
print make_randint_list(10, 99, 10, True, True)

実行結果

$ python makerand0_test.py
[52, 44, 63, 61, 50, 66, 88, 45, 99, 57]
[15, 26, 29, 53, 56, 69, 89, 91, 94, 95]
[96, 95, 93, 88, 79, 75, 64, 62, 33, 11]

リファレンス

チュートリアル

関連記事


 Sponsored Link

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.yukun.info/blog/2008/06/python-random2.html/trackback
Listed below are links to weblogs that reference
Python: モジュールにテスト関数を定義 - 重複のない乱数(整数MIN以上MAX以下)の生成 from Yukun's Blog

Home > Python > Python: モジュールにテスト関数を定義 - 重複のない乱数(整数MIN以上MAX以下)の生成

バックナンバー
最近のコメント
最近のトラックバック
メタ情報

Return to page top