challenge n人中m人が当選するくじ

n人の中から公平にm人を選ぶ、くじ引きプログラムを作ってください。

Posted feedbacks - Groovy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def lottery(n,m) {
  rand = new Random()

  all = 0..<n
  (1..m).inject([]) { wins, _bottom ->
    win = all[rand.nextInt(all.size())]
    all -= win
    wins + win
  }
}

println lottery(4,1)
println lottery(4,2)
println lottery(4,3)
println lottery(4,4)

いまさら投稿。

1
2
3
4
5
6
7
def select(n, m){
    def list = 1..n as ArrayList
    Collections.shuffle list
    list[0..<m]
}

println select(5, 3)

Index

Feed

Other

Link

Pathtraq

loading...