R
Roland Rau
Dear all,
I am looking for a function in C which does the following:
After giving an array of n integers ranging from 1 to n, the function
should return a random sample of size k (k <= n) without replacement.
This is similar to take a random permutation of that array and then
choose the first k elements.
Some code example might clarify what I mean.
In R and S-Plus, the command would be:
sample(x=1:n, size = k, replace=FALSE)
In GNU Octave it would be (probably the same then in Matlab):
randperm(n)(1:k)
In Python, the command would be:
import random
random.sample(range(1,n+1), k)
I am pretty new to C, so I hope this is the correct forum to address
this question. I know this is nothing special, but I don't know so far
where to look up things for C (which is probably better than my first
humble approaches to implement such a function on my own).
Any advice is greatly appreciated (URLs, code fragments, ...).
Thanks,
Roland
(please change "HRO" into "rostock" to obtain my email address.
I am looking for a function in C which does the following:
After giving an array of n integers ranging from 1 to n, the function
should return a random sample of size k (k <= n) without replacement.
This is similar to take a random permutation of that array and then
choose the first k elements.
Some code example might clarify what I mean.
In R and S-Plus, the command would be:
sample(x=1:n, size = k, replace=FALSE)
In GNU Octave it would be (probably the same then in Matlab):
randperm(n)(1:k)
In Python, the command would be:
import random
random.sample(range(1,n+1), k)
I am pretty new to C, so I hope this is the correct forum to address
this question. I know this is nothing special, but I don't know so far
where to look up things for C (which is probably better than my first
humble approaches to implement such a function on my own).
Any advice is greatly appreciated (URLs, code fragments, ...).
Thanks,
Roland
(please change "HRO" into "rostock" to obtain my email address.