Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@Hirosaji

繰り返しを許さない組み合わせをランダムで出力

More than 3 years have passed since last update.

一対比較法を用いたデザインの比較実験の過程で作りました。
順序効果を打ち消すために、実験対象(デザイン)の組み合わせをランダム提示するコードです。

ソースコード

random_list.py
import random

#デザインパターンの数
design_num = 3

#デザインパターンの組み合わせリストを作成
numbers1 = numbers2 = range(1,design_num+1)
pair_list = [[m, n] for m in numbers1 for n in numbers2 if(m!=n)]

#ランダムで組み合わせを出力
print(random.sample(pair_list, design_num*(design_num-1)))

実行結果

[[3,2], [1,2], [3,1], [1,3], [2,3], [2,1]]

後輩からの急ぎの注文で、下調べせずサクッと書いたコードです。
もしかしたら、もっと上手い書き方や便利なライブラリがあるかも。

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Hirosaji
Webエンジニア/イラストレーター。Webと可視化とゲーム実況が好き。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?