i have to generate random numbers from length 2 to 30 and length two should have the numbers 1,2 and length 3 should have the numbers 1,2,3 so on and so forth till size 30.i should get an sequence like 2222221 or 1111112 till both the values appear and for 3 it should be 1222223 or 3331112.i should get similar output for all sizes which means i should get 30 sequences.but am getting only one sequence .please help. this is my code y=0; for (i in 1:3) { while(y!=i) { y=sample(1:3,replace=TRUE); } } -- View this message in context: http://www.nabble.com/R-random-number-generation-tp20138441p20138441.html Sent from the R help mailing list archive at Nabble.com.
On 23-Oct-08 19:58:12, hiphop wrote:> > i have to generate random numbers from length 2 to 30 and > length two should have the numbers 1,2 and length 3 should > have the numbers 1,2,3 so on and so forth till size 30. > i should get an sequence like 2222221 or 1111112 till both > the values appear and for 3 it should be 1222223 or 3331112. > i should get similar output for all sizes which means i should > get 30 sequences.but am getting only one sequence. > please help. this is my code > > y=0; > for (i in 1:3) > { > while(y!=i) > { > y=sample(1:3,replace=TRUE); > } > }If I understand you correctly, the following should do what you are looking for (i.e. sample repeatedly from (1:k) until you first have all of 1:k in the sample, repetitions being allowed): mksample <- function(k){ All <- (1:k) ; Left <- rep(TRUE,k) ; Done <- FALSE Result <- NULL while(! Done){ j <- sample(All,1) Result <- c(Result, j); Left[j] <- FALSE Done <- (sum(Left)==0) } Result } Example: mksample(5) # [1] 1 4 4 5 2 1 1 3 I'm not clear about why, in your illustrations (such as "3331112") you presented it as a sequence of runs ("333", "111","2") -- maybe this is not relevant to your query. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 23-Oct-08 Time: 21:51:04 ------------------------------ XFMail ------------------------------
hi thanks for the reply actually am supposed to generate random numbers in oncreasing size. so when i say 1,2 i should get something like 12222 or 221 or 1112 ...i should get both the values 1 and 2 in a random order but it should stop when both the values occur atleast once in the sequence.so after 1,2 it should be 1,2,3 and the sequence generated should have 1,2,3 atleast once.then it should generate a random sequence of numbers 1,2,3,4 ...it should go on till it generates 1,2....30.hope am clear now. hiphop wrote:> > i have to generate random numbers from length 2 to 30 and length two > should have the numbers 1,2 and length 3 should have the numbers 1,2,3 so > on and so forth till size 30.i should get an sequence like 2222221 or > 1111112 till both the values appear and for 3 it should be 1222223 or > 3331112.i should get similar output for all sizes which means i should get > 30 sequences.but am getting only one sequence .please help. > this is my code > > y=0; > for (i in 1:3) > { > while(y!=i) > { > y=sample(1:3,replace=TRUE); > } > } > >-- View this message in context: http://www.nabble.com/R-random-number-generation-tp20138441p20142412.html Sent from the R help mailing list archive at Nabble.com.
below i think semi does what you want but it's never going to repeat a value because it's sampling without replacement ? in that sense, it's not as general as what you asked for. if you need the repeating thing, then i'm not sure how to do that. hopefully someone else does. lapply(1:30, function(.index) { sample(1:.index,replace=FALSE,size=.index) }) On Thu, Oct 23, 2008 at 8:53 PM, hiphop wrote:> hi > > thanks for the reply > actually am supposed to generate random numbers in oncreasing size. > so when i say 1,2 i should get something like 12222 or 221 or 1112 > ...i > should get both the values 1 and 2 in a random order but it should > stop when > both the values occur atleast once in the sequence.so after 1,2 it > should be > 1,2,3 and the sequence generated should have 1,2,3 atleast once.then > it > should generate a random sequence of numbers 1,2,3,4 ...it should go > on till > it generates 1,2....30.hope am clear now. > > > > hiphop wrote: >> >> i have to generate random numbers from length 2 to 30 and length two >> should have the numbers 1,2 and length 3 should have the numbers >> 1,2,3 so >> on and so forth till size 30.i should get an sequence like 2222221 or >> 1111112 till both the values appear and for 3 it should be 1222223 or >> 3331112.i should get similar output for all sizes which means i >> should get >> 30 sequences.but am getting only one sequence .please help. >> this is my code >> >> y=0; >> for (i in 1:3) >> { >> while(y!=i) >> { >> y=sample(1:3,replace=TRUE); >> } >> } >> >> > > -- > View this message in context: > http://www.nabble.com/R-random-number-generation-tp20138441p20142412.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.