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

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

Posted feedbacks - Matlab

やっていることはMathematica版と同じですね。

出力例:
>> lot(100, 10)
ans =
    80    42    71    97    51     4    81    94     2    92
1
2
3
4
function x = lot(n, m)
  A = randperm(n);
  x = A(1:m);
end

もうすこし調べたら、そのものずばりの関数がありました。がっかり。

出力例:
>> randsample(100,10)
ans =
    96
     6
    71
    46
    81
    56
    34
    22
    80
    88
1
randsample(n,m)

Index

Feed

Other

Link

Pathtraq

loading...