SlideShare a Scribd company logo
1 of 5
Download to read offline
ランダム・シャッフル

    まるやま



   2013/04/18




                1/5
問題




     1から10までの値が、順番に配列に入っている
     これをランダムに並べ替えたい
       こんなことが必要なこともあるよね
     さて、どうすればよいか




                              2/5
考え方
  配列の並べ替えなので、結局は配列要素をスワップする
  {どうやって|何回}並べ替えるのかが、考えるポイント
  配列の取り得るパターン数は、順列数え上げなので、
  N 個の配列だとすると N PN = N! 個
  irandom(i,j) という関数は、
  i 以上 j 以下のランダムな整数を返すものとする
  iramdom(1,N) を行って、1 以上 N 以下の値 r 1 を得る
  最初は配列要素の位置と値が同じはずなので、
  1番目の要素と r 1 番目の要素を入れ替えることによって、
  1番目の要素をランダムな値にする
      1番目の要素が選ばれるパターンは、N 通り
  irandom(2,N) を行って、2 以上 N 以下の値 r 2 を得る
  2番目の要素と r 2 番目の要素を入れ替えることによって、
  2番目の要素を(1番目の値以外の)ランダムな値にする
      2番目の要素が選ばれるパターンは、N − 1 通り
                                          3/5
考え方(続き)




  このやり方を N − 1 回繰り返す
    N − 1 回目は、irandom(N-1,N) なので、
    N − 1 番目と N 番目を入れ替えるか否かの2択
    N 回目は、もうひとつしか残っていないので、やる必要なし
  すると、ランダムに選んだ方法のパターン数も、N! 通り
  配列のパターン数と同じだけ、ランダムに並べ替えられる




                                    4/5
Lua で実装

 Lua で実装してみる
 random shuffle.lua
 num = 10
 math.randomseed(os.time())
 a = {}
 for i = 1, num, 1 do a[i] = i end
 for i = 1, num - 1, 1 do
   r = math.random(i, num)
   if (i < r) then a[i], a[r] = a[r], a[i] end
 end
 for i = 1, num, 1 do print(a[i]) end

 Lua にはお誂え向きの random も swap もある!
 ほとんどワンライナーじゃないか!


                                                 5/5

More Related Content

What's hot

Ruby紹介3(pdf)
Ruby紹介3(pdf)Ruby紹介3(pdf)
Ruby紹介3(pdf)Gohryuh
 
15パズルを作ってみた
15パズルを作ってみた15パズルを作ってみた
15パズルを作ってみたHota Oku
 
03 var array_flow_func
03 var array_flow_func03 var array_flow_func
03 var array_flow_func文樹 高橋
 
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1Computational Materials Science Initiative
 

What's hot (7)

Ruby紹介3(pdf)
Ruby紹介3(pdf)Ruby紹介3(pdf)
Ruby紹介3(pdf)
 
15パズルを作ってみた
15パズルを作ってみた15パズルを作ってみた
15パズルを作ってみた
 
C言語講習会3
C言語講習会3C言語講習会3
C言語講習会3
 
03 var array_flow_func
03 var array_flow_func03 var array_flow_func
03 var array_flow_func
 
たのしい関数型
たのしい関数型たのしい関数型
たのしい関数型
 
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1
CMSI計算科学技術特論A(6) 線形代数演算ライブラリBLASとLAPACKの基礎と実践1
 
C言語講習会4
C言語講習会4C言語講習会4
C言語講習会4
 

Viewers also liked

shot note で手書き資料作成
shot note で手書き資料作成shot note で手書き資料作成
shot note で手書き資料作成Maruyama Tetsutaro
 
円錐曲線の極座標表示
円錐曲線の極座標表示円錐曲線の極座標表示
円錐曲線の極座標表示Maruyama Tetsutaro
 
Zipf分布に従う乱数の生成方法
Zipf分布に従う乱数の生成方法Zipf分布に従う乱数の生成方法
Zipf分布に従う乱数の生成方法Maruyama Tetsutaro
 
Online Matching and Ad Allocaton 8章&9章半分
Online Matching and Ad Allocaton 8章&9章半分Online Matching and Ad Allocaton 8章&9章半分
Online Matching and Ad Allocaton 8章&9章半分Maruyama Tetsutaro
 
ユークリッド距離以外の距離で教師無しクラスタリング
ユークリッド距離以外の距離で教師無しクラスタリングユークリッド距離以外の距離で教師無しクラスタリング
ユークリッド距離以外の距離で教師無しクラスタリングMaruyama Tetsutaro
 
集中不等式のすすめ [集中不等式本読み会#1]
集中不等式のすすめ [集中不等式本読み会#1]集中不等式のすすめ [集中不等式本読み会#1]
集中不等式のすすめ [集中不等式本読み会#1]Kentaro Minami
 
How to Develop Experiment-Oriented Programs
How to Develop Experiment-Oriented ProgramsHow to Develop Experiment-Oriented Programs
How to Develop Experiment-Oriented ProgramsKenta Oono
 
機械学習向けプログラミング言語の使い分け - RCO の場合
機械学習向けプログラミング言語の使い分け - RCO の場合機械学習向けプログラミング言語の使い分け - RCO の場合
機械学習向けプログラミング言語の使い分け - RCO の場合Maruyama Tetsutaro
 
Stochastic Gradient MCMC
Stochastic Gradient MCMCStochastic Gradient MCMC
Stochastic Gradient MCMCKenta Oono
 
高速・省メモリにlibsvm形式で ダンプする方法を研究してみた
高速・省メモリにlibsvm形式で ダンプする方法を研究してみた高速・省メモリにlibsvm形式で ダンプする方法を研究してみた
高速・省メモリにlibsvm形式で ダンプする方法を研究してみたKeisuke Hosaka
 
Introduction of “Fairness in Learning: Classic and Contextual Bandits”
Introduction of “Fairness in Learning: Classic and Contextual Bandits”Introduction of “Fairness in Learning: Classic and Contextual Bandits”
Introduction of “Fairness in Learning: Classic and Contextual Bandits”Kazuto Fukuchi
 
Differential privacy without sensitivity [NIPS2016読み会資料]
Differential privacy without sensitivity [NIPS2016読み会資料]Differential privacy without sensitivity [NIPS2016読み会資料]
Differential privacy without sensitivity [NIPS2016読み会資料]Kentaro Minami
 
Conditional Image Generation with PixelCNN Decoders
Conditional Image Generation with PixelCNN DecodersConditional Image Generation with PixelCNN Decoders
Conditional Image Generation with PixelCNN Decoderssuga93
 
Interaction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsInteraction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsKen Kuroki
 
Introduction of "TrailBlazer" algorithm
Introduction of "TrailBlazer" algorithmIntroduction of "TrailBlazer" algorithm
Introduction of "TrailBlazer" algorithmKatsuki Ohto
 
Fast and Probvably Seedings for k-Means
Fast and Probvably Seedings for k-MeansFast and Probvably Seedings for k-Means
Fast and Probvably Seedings for k-MeansKimikazu Kato
 
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...Shuhei Yoshida
 
Improving Variational Inference with Inverse Autoregressive Flow
Improving Variational Inference with Inverse Autoregressive FlowImproving Variational Inference with Inverse Autoregressive Flow
Improving Variational Inference with Inverse Autoregressive FlowTatsuya Shirakawa
 

Viewers also liked (20)

shot note で手書き資料作成
shot note で手書き資料作成shot note で手書き資料作成
shot note で手書き資料作成
 
keynoteでslideshare
keynoteでslidesharekeynoteでslideshare
keynoteでslideshare
 
円錐曲線の極座標表示
円錐曲線の極座標表示円錐曲線の極座標表示
円錐曲線の極座標表示
 
Zipf分布に従う乱数の生成方法
Zipf分布に従う乱数の生成方法Zipf分布に従う乱数の生成方法
Zipf分布に従う乱数の生成方法
 
配列数式
配列数式配列数式
配列数式
 
Online Matching and Ad Allocaton 8章&9章半分
Online Matching and Ad Allocaton 8章&9章半分Online Matching and Ad Allocaton 8章&9章半分
Online Matching and Ad Allocaton 8章&9章半分
 
ユークリッド距離以外の距離で教師無しクラスタリング
ユークリッド距離以外の距離で教師無しクラスタリングユークリッド距離以外の距離で教師無しクラスタリング
ユークリッド距離以外の距離で教師無しクラスタリング
 
集中不等式のすすめ [集中不等式本読み会#1]
集中不等式のすすめ [集中不等式本読み会#1]集中不等式のすすめ [集中不等式本読み会#1]
集中不等式のすすめ [集中不等式本読み会#1]
 
How to Develop Experiment-Oriented Programs
How to Develop Experiment-Oriented ProgramsHow to Develop Experiment-Oriented Programs
How to Develop Experiment-Oriented Programs
 
機械学習向けプログラミング言語の使い分け - RCO の場合
機械学習向けプログラミング言語の使い分け - RCO の場合機械学習向けプログラミング言語の使い分け - RCO の場合
機械学習向けプログラミング言語の使い分け - RCO の場合
 
Stochastic Gradient MCMC
Stochastic Gradient MCMCStochastic Gradient MCMC
Stochastic Gradient MCMC
 
高速・省メモリにlibsvm形式で ダンプする方法を研究してみた
高速・省メモリにlibsvm形式で ダンプする方法を研究してみた高速・省メモリにlibsvm形式で ダンプする方法を研究してみた
高速・省メモリにlibsvm形式で ダンプする方法を研究してみた
 
Introduction of “Fairness in Learning: Classic and Contextual Bandits”
Introduction of “Fairness in Learning: Classic and Contextual Bandits”Introduction of “Fairness in Learning: Classic and Contextual Bandits”
Introduction of “Fairness in Learning: Classic and Contextual Bandits”
 
Differential privacy without sensitivity [NIPS2016読み会資料]
Differential privacy without sensitivity [NIPS2016読み会資料]Differential privacy without sensitivity [NIPS2016読み会資料]
Differential privacy without sensitivity [NIPS2016読み会資料]
 
Conditional Image Generation with PixelCNN Decoders
Conditional Image Generation with PixelCNN DecodersConditional Image Generation with PixelCNN Decoders
Conditional Image Generation with PixelCNN Decoders
 
Interaction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and PhysicsInteraction Networks for Learning about Objects, Relations and Physics
Interaction Networks for Learning about Objects, Relations and Physics
 
Introduction of "TrailBlazer" algorithm
Introduction of "TrailBlazer" algorithmIntroduction of "TrailBlazer" algorithm
Introduction of "TrailBlazer" algorithm
 
Fast and Probvably Seedings for k-Means
Fast and Probvably Seedings for k-MeansFast and Probvably Seedings for k-Means
Fast and Probvably Seedings for k-Means
 
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...
InfoGAN: Interpretable Representation Learning by Information Maximizing Gen...
 
Improving Variational Inference with Inverse Autoregressive Flow
Improving Variational Inference with Inverse Autoregressive FlowImproving Variational Inference with Inverse Autoregressive Flow
Improving Variational Inference with Inverse Autoregressive Flow
 

More from Maruyama Tetsutaro

Mining of massive datasets chapter3
Mining of massive datasets chapter3Mining of massive datasets chapter3
Mining of massive datasets chapter3Maruyama Tetsutaro
 
業務に活かすデータサイエンスとは?
業務に活かすデータサイエンスとは?業務に活かすデータサイエンスとは?
業務に活かすデータサイエンスとは?Maruyama Tetsutaro
 
Ubuntuで最新パッケージを導入
Ubuntuで最新パッケージを導入Ubuntuで最新パッケージを導入
Ubuntuで最新パッケージを導入Maruyama Tetsutaro
 
Zshでデキるプロンプト
ZshでデキるプロンプトZshでデキるプロンプト
ZshでデキるプロンプトMaruyama Tetsutaro
 

More from Maruyama Tetsutaro (7)

Lambda and rundeck
Lambda and rundeckLambda and rundeck
Lambda and rundeck
 
Mining of massive datasets chapter3
Mining of massive datasets chapter3Mining of massive datasets chapter3
Mining of massive datasets chapter3
 
業務に活かすデータサイエンスとは?
業務に活かすデータサイエンスとは?業務に活かすデータサイエンスとは?
業務に活かすデータサイエンスとは?
 
日本の伝統色
日本の伝統色日本の伝統色
日本の伝統色
 
Gnuplotあれこれ
GnuplotあれこれGnuplotあれこれ
Gnuplotあれこれ
 
Ubuntuで最新パッケージを導入
Ubuntuで最新パッケージを導入Ubuntuで最新パッケージを導入
Ubuntuで最新パッケージを導入
 
Zshでデキるプロンプト
ZshでデキるプロンプトZshでデキるプロンプト
Zshでデキるプロンプト
 

ランダム・シャッフル

  • 1. ランダム・シャッフル まるやま 2013/04/18 1/5
  • 2. 問題 1から10までの値が、順番に配列に入っている これをランダムに並べ替えたい こんなことが必要なこともあるよね さて、どうすればよいか 2/5
  • 3. 考え方 配列の並べ替えなので、結局は配列要素をスワップする {どうやって|何回}並べ替えるのかが、考えるポイント 配列の取り得るパターン数は、順列数え上げなので、 N 個の配列だとすると N PN = N! 個 irandom(i,j) という関数は、 i 以上 j 以下のランダムな整数を返すものとする iramdom(1,N) を行って、1 以上 N 以下の値 r 1 を得る 最初は配列要素の位置と値が同じはずなので、 1番目の要素と r 1 番目の要素を入れ替えることによって、 1番目の要素をランダムな値にする 1番目の要素が選ばれるパターンは、N 通り irandom(2,N) を行って、2 以上 N 以下の値 r 2 を得る 2番目の要素と r 2 番目の要素を入れ替えることによって、 2番目の要素を(1番目の値以外の)ランダムな値にする 2番目の要素が選ばれるパターンは、N − 1 通り 3/5
  • 4. 考え方(続き) このやり方を N − 1 回繰り返す N − 1 回目は、irandom(N-1,N) なので、 N − 1 番目と N 番目を入れ替えるか否かの2択 N 回目は、もうひとつしか残っていないので、やる必要なし すると、ランダムに選んだ方法のパターン数も、N! 通り 配列のパターン数と同じだけ、ランダムに並べ替えられる 4/5
  • 5. Lua で実装 Lua で実装してみる random shuffle.lua num = 10 math.randomseed(os.time()) a = {} for i = 1, num, 1 do a[i] = i end for i = 1, num - 1, 1 do r = math.random(i, num) if (i < r) then a[i], a[r] = a[r], a[i] end end for i = 1, num, 1 do print(a[i]) end Lua にはお誂え向きの random も swap もある! ほとんどワンライナーじゃないか! 5/5