[R] Randomly rearranging elements of sets
Rolf Turner
r.turner at auckland.ac.nz
Thu Jan 28 00:11:54 CET 2010
?sample
On 28/01/2010, at 12:06 PM, jshort wrote:
>
>
> Hi
>
> I've recently been trying to solve some probability questions in R,
> but am
> having trouble.
>
> This is one question thats been causing some hair loss:
>
> Given the set of integers S = {1,2,3,4,5,6,7,8,9,10}, create a
> function F(S)
> that uniformly and randomly rearranges these integers.
>
> For example, in one instance, we might have F(S) =
> {2,3,9,7,5,6,1,8,10,4}.
>
> ......................................................................
> ..................................................
>
> Here is what I've managed so far with regards to solving the above.
>
> The first part of my program is just the following (which is
> flawless):
>
>
> "w = matrix(0, ncol = 10, nrow = 1, byrow = T)
> for( i in 1:1) {
> (m = runif(1,0,10))
> for(j in 1:10) { if (j-1 < m & m < j)
> w[1,i] = j }"
>
> As you can see, I first define my output (called w) as "1 by 10"
> matrix.
>
> Afterwards, I randomly assign the first integer '1' to one of the
> positions
> on 1-10 (the function "runif(1,0,10)" lets me do this so that each
> position
> is equally likely).
>
>
> The next part of my program is where the difficulty lies. We need to
> randomly assign the remaining 9 integers. The program should look
> something
> like the following:
>
> for( i in 2:10) {
> (q = runif(1,0,10)
> { if(condition(i)) w[1,i] = xxx, else (repeat with new q-value untill
> condition(i) is met)
> }
>
> I know how to fill one of the details in the above. Clearly
> "condition(i)"
> should something like the following:
>
>
>
> for(e in 2:10) { if (e-1 < q & q < e) &
> (for(s in 1:i-1) { e != w[1,s] } w[1,i] = e
>
>
> The REAL issue is filling the exact details of the "else"
> condition. Clearly
> I want to tell R that I wish to repeat with a new random number "q"
> until
> condition(i) is met, but I don't know how to tell R this.
>
> Any help with this would be much appreciated.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list