Re: [R] random number generation
Dear Leila Does the function mvrnorm in package MASS do what you want? On 11/11/2015 08:38, leila zamani via R-help wrote: Hi every one, I'm new to R. I read about R and search all the packages but I couldn't find the package that I want. I want to generate 2D (matrices) random numbers that have correlation between them. One of my friends said that R has a function in geor like rand...(spherical (correlation coefficient=0.5)), but I read all packages with geo,like geor,geoRlgm,georob and etc, but I didn't find this! Can you help me? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. -- Michael http://www.dewey.myzen.co.uk/home.html __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] random number generation
Hi, This generate matrix: matrix( rnorm(5*4,mean=0,sd=1), 5, 4) for correlated matrix please see this link: http://stackoverflow.com/questions/10680658/how-can-i-create-a-correlation-matrix-in-r Karim On Wed, Nov 11, 2015 at 9:38 AM, leila zamani via R-help < r-help@r-project.org> wrote: > Hi every one, > I'm new to R. I read about R and search all the packages but I couldn't > find the package that I want. I want to generate 2D (matrices) random > numbers that have correlation between them. One of my friends said that R > has a function in geor like rand...(spherical (correlation > coefficient=0.5)), but I read all packages with geo,like > geor,geoRlgm,georob and etc, but I didn't find this! Can you help me? > Thanks. > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] random number generation
Hi every one, I'm new to R. I read about R and search all the packages but I couldn't find the package that I want. I want to generate 2D (matrices) random numbers that have correlation between them. One of my friends said that R has a function in geor like rand...(spherical (correlation coefficient=0.5)), but I read all packages with geo,like geor,geoRlgm,georob and etc, but I didn't find this! Can you help me? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Random number generation
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Dennis Murphy Sent: Thursday, July 21, 2011 8:55 PM To: karena Cc: r-help@r-project.org Subject: Re: [R] Random number generation Hi: Here's one way using uniform(0, 1) pseudo-random numbers, but there are many ways you could go about this. # each row comprises a set of three pseudo-random numbers u - matrix(runif(30), nrow = 10) # divide each element in a row by its row sum v - t(apply(u, 1, function(x) x/sum(x))) rowSums(v) [1] 1 1 1 1 1 1 1 1 1 1 # An equivalent way (about equally fast) is u/outer(rowSums(u), rep(1, 3)) Now try hist(unlist(v)) and notice that the distribution of the constrained sets is not really uniform. This is a consequence of setting a constraint on the sum of each sample. Another way to see this is to plot plot(sort(unlist(v))) A 'truly' uniform random sample would lie approximately on a straight line in this plot. It would seem to me that a better approach would be to sample from a simplex embedded in the unit cube. I'd suggest looking into the compositions package (because you are effectively generating compositional data) and look into its capabilities. At least a couple of the references in the package's overview page seem to be germane to the problem. The pair of runif.* functions appear to be relevant. HTH, Dennis On Thu, Jul 21, 2011 at 4:18 PM, karena dr.jz...@gmail.com wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? thank you, karena Karena, if you search the R-help archive you will find a couple of threads discussing this topic. Here is one URL to get you started. http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2520.html Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ R-help@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.
Re: [R] Random number generation
Thank you guys for all the help. I appreciate! -- View this message in context: http://r.789695.n4.nabble.com/Random-number-generation-tp3685463p3686392.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Random number generation
Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? thank you, karena -- View this message in context: http://r.789695.n4.nabble.com/Random-number-generation-tp3685463p3685463.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Random number generation
On 07/21/2011 07:18 PM, karena wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? Try this: df-data.frame(a=runif(10),b=runif(10)) df$c=df$a+df$b df Replace 10 with the number of rows that you want. Replace runif with your random function of choice. Jason __ R-help@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.
Re: [R] Random number generation
On 07/21/2011 08:04 PM, Jason Edgecombe wrote: On 07/21/2011 07:18 PM, karena wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? Try this: df-data.frame(a=runif(10),b=runif(10)) df$c=df$a+df$b df Replace 10 with the number of rows that you want. Replace runif with your random function of choice. Jason Nevermind, I didn't address how to ensure that each row adds up to one. Sorry, Jason __ R-help@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.
Re: [R] Random number generation
On 21/07/2011 7:18 PM, karena wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? There are lots of ways. What is the context? It would determine the first two steps: 1. Generate A. 2. Generate B. 3. C - 1-A-B Duncan Murdoch __ R-help@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.
Re: [R] Random number generation
Could this possibly be a homework question? cheers, Rolf Turner On 22/07/11 11:18, karena wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? __ R-help@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.
Re: [R] Random number generation
To Jason: Still thank you, your reply gives me some clue about how to do this. To Duncan: Thank you for rely. It helped. Karena -- View this message in context: http://r.789695.n4.nabble.com/Random-number-generation-tp3685463p3685821.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Random number generation
Hi: Here's one way using uniform(0, 1) pseudo-random numbers, but there are many ways you could go about this. # each row comprises a set of three pseudo-random numbers u - matrix(runif(30), nrow = 10) # divide each element in a row by its row sum v - t(apply(u, 1, function(x) x/sum(x))) rowSums(v) [1] 1 1 1 1 1 1 1 1 1 1 # An equivalent way (about equally fast) is u/outer(rowSums(u), rep(1, 3)) Now try hist(unlist(v)) and notice that the distribution of the constrained sets is not really uniform. This is a consequence of setting a constraint on the sum of each sample. Another way to see this is to plot plot(sort(unlist(v))) A 'truly' uniform random sample would lie approximately on a straight line in this plot. It would seem to me that a better approach would be to sample from a simplex embedded in the unit cube. I'd suggest looking into the compositions package (because you are effectively generating compositional data) and look into its capabilities. At least a couple of the references in the package's overview page seem to be germane to the problem. The pair of runif.* functions appear to be relevant. HTH, Dennis On Thu, Jul 21, 2011 at 4:18 PM, karena dr.jz...@gmail.com wrote: Hi, I want to generate multiple sets of random numbers. The requirement is that: 1) each set have 3 random numbers; 2) the sum of the three number is always 1. how to do this? thank you, karena -- View this message in context: http://r.789695.n4.nabble.com/Random-number-generation-tp3685463p3685463.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
[R] Random Number Generation in a Loop
Dear R helpers I am having following data Name Numbers A 25 B 3 C 13 A 5 B 7 C 0 A 2 B 10 C 9 CONDITIONS If Name is A, min_val = 1.05, max_val = 1.30 If Name is B, min_val = 1.30, max_val = 1.60 If Name is C, min_val = 1.60, max_val = 1.99 TASK To generate the Uniform random nos for each of these Names (Equal to the corresponding no. e.g. the 5th Name is B, so I need to generate 7 uniform random numbers in the range (1.30 - 1.99). Also I need to arrange these random numbers one by one in a single csv file i.e. say 25 random numbers in teh range (1.05-1.30) followed by 3 random numbers in the range (1.30-1.60) and so on. # ___ Here is the R code I have tried ONS - read.table(textConnection(name number A11 12 A12 17 A13 0 A11 11 A12 6 A13 0 A11 8 A12 4 A13 3), header = TRUE) X = as.character(ONS$name) Y = ONS$number Z = NULL for (i in 1:length(X)) { if(X[i] == 'A11') { min_val = 1.05 max_val = 1.30 Z = runif(Y[i], min_val, max_val) } else { if(X[i] == 'A12') { min_val = 1.30 max_val = 1.60 Z = runif(Y[i], min_val, max_val) } else { if(X[i] == 'A13') { min_val = 1.60 max_val = 1.99 Z = runif(Y[i], min_val, max_val) } } } } # End of Code ## _ PROBLEM I need to get 61 random numbers which is total of all the numbers (1st 12 fo A, 3 random numbers for B, 13 for C, 5 again fo A and so on). The result whcih I got is Z [1] 1.740443 1.761758 1.797222 which is pertaining to the last name C where 3 random numbers are generated i.e. Z instaed of getting added, is overwritten. Please help me to rectify my code so that in the end I will get 61 random numbers as desired i.e. 12 for A in the range (1.05 - 1.30), 3 for B in the range (1.30 - 1.60), 13 for C in the range (1.60-1.99), again 5 for A in the range (1.05 - 1.30). Thanking in advance. I also sincerely apologize for writing such a long mail, as I wanted to be clear as possible in my communication. Regards Maithili The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Random Number Generation in a Loop
Hi, does this do what you want to do? I take the first six lines of the data you provided. Note that letters corresponds to your Name, and number corresponds to your Numbers variable. letters=rep(c(A,B,C),2) letters number=c(25,3,13,5,7,0) number letters2=rep(letters,number) letters2 interval.min=ifelse(letters2==A,1.05,ifelse(letters2==B,1.3,1.6)) interval.max=ifelse(letters2==A,1.3,ifelse(letters2==B,1.6,1.99)) rand.nums=runif(letters2,min=interval.min,max=interval.max) data.frame(letters2,rand.nums) HTH, Daniel - cuncta stricte discussurus - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Maithili Shiva Sent: Thursday, December 17, 2009 1:25 AM To: r-help@r-project.org Subject: [R] Random Number Generation in a Loop Dear R helpers I am having following data Name Numbers A 25 B 3 C 13 A 5 B 7 C 0 A 2 B 10 C 9 CONDITIONS If Name is A, min_val = 1.05, max_val = 1.30 If Name is B, min_val = 1.30, max_val = 1.60 If Name is C, min_val = 1.60, max_val = 1.99 TASK To generate the Uniform random nos for each of these Names (Equal to the corresponding no. e.g. the 5th Name is B, so I need to generate 7 uniform random numbers in the range (1.30 - 1.99). Also I need to arrange these random numbers one by one in a single csv file i.e. say 25 random numbers in teh range (1.05-1.30) followed by 3 random numbers in the range (1.30-1.60) and so on. # ___ Here is the R code I have tried ONS - read.table(textConnection(name number A1112 A1217 A13 0 A1111 A12 6 A13 0 A11 8 A12 4 A13 3), header = TRUE) X = as.character(ONS$name) Y = ONS$number Z = NULL for (i in 1:length(X)) { if(X[i] == 'A11') { min_val = 1.05 max_val = 1.30 Z = runif(Y[i], min_val, max_val) } else { if(X[i] == 'A12') { min_val = 1.30 max_val = 1.60 Z = runif(Y[i], min_val, max_val) } else { if(X[i] == 'A13') { min_val = 1.60 max_val = 1.99 Z = runif(Y[i], min_val, max_val) } } } } # End of Code ## _ PROBLEM I need to get 61 random numbers which is total of all the numbers (1st 12 fo A, 3 random numbers for B, 13 for C, 5 again fo A and so on). The result whcih I got is Z [1] 1.740443 1.761758 1.797222 which is pertaining to the last name C where 3 random numbers are generated i.e. Z instaed of getting added, is overwritten. Please help me to rectify my code so that in the end I will get 61 random numbers as desired i.e. 12 for A in the range (1.05 - 1.30), 3 for B in the range (1.30 - 1.60), 13 for C in the range (1.60-1.99), again 5 for A in the range (1.05 - 1.30). Thanking in advance. I also sincerely apologize for writing such a long mail, as I wanted to be clear as possible in my communication. Regards Maithili The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[alternative HTML version deleted]] __ R-help@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.
[R] Random number generation for Generalized Logistic distribution
Dear R helpers How to generate random numbers for (a) Generalized logistic distribution (b) Generalized normal distribution (c) Pearson Type III distribution (d) Kappa Thanks in advance Maithili __ R-help@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.
Re: [R] Random number generation for Generalized Logistic distribution
See the lmom package by J.R.M Hosking or lmomco package by W.H.Asquith -wha On Feb 24, 2009, at 5:46 AM, Maithili Shiva wrote: Dear R helpers How to generate random numbers for (a) Generalized logistic distribution (b) Generalized normal distribution (c) Pearson Type III distribution (d) Kappa Thanks in advance Maithili __ R-help@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. __ R-help@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.
[R] Random Number Generation using (Generalized) Extreme Value distribution and Pareto distribution
Hi R helpers, Is there any function in R, which generates random numbers in case of (1) Generalized Extreme Value distribution and (2) Generalized PAreto distribution for the respective given set of parameters? Regards Maithili __ R-help@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.
Re: [R] Random Number Generation using (Generalized) Extreme Value distribution and Pareto distribution
Dear Maithili, For (1) see [1] and for (2) see page 24 in [2]. HTH, Jorge [1] http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/evd/html/gev.html [2] http://cran.r-project.org/web/packages/fExtremes/fExtremes.pdf On Thu, Dec 18, 2008 at 3:17 AM, Maithili Shiva maithili_sh...@yahoo.comwrote: Hi R helpers, Is there any function in R, which generates random numbers in case of (1) Generalized Extreme Value distribution and (2) Generalized PAreto distribution for the respective given set of parameters? Regards Maithili __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
[R] R random number generation
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 221 or 112 till both the values appear and for 3 it should be 123 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. __ R-help@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.
Re: [R] R random number generation
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 221 or 112 till both the values appear and for 3 it should be 123 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) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 094 0861 Date: 23-Oct-08 Time: 21:51:04 -- XFMail -- __ R-help@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.
Re: [R] R random number generation
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 1 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,230.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 221 or 112 till both the values appear and for 3 it should be 123 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@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.
Re: [R] R random number generation
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 1 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,230.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 221 or 112 till both the values appear and for 3 it should be 123 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@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. __ R-help@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.
Re: [R] Random number generation
Please don't cross-post. I've replied in R-devel. On 8/5/2008 8:47 AM, subramanian R wrote: Hi All, I have a few queries regarding Random Number generation in R. according to the help(Random.User) i defined my own functions for user_unif_rand and user_norm_rand (uniform and normal distribution) But what i figured out was even when i call rexp,rpois,rgeom and other distributions they were routed via user_unif_rand. 1. Does this mean that for all types of distribution it generates an Uniform distribution and transforms to the requested type? Also surprisingly even rnorm which i hoped to route via user_norm_rand was calling user_unif_rand... rnorm(1) inside user_unif_rand//printf and inside user_unif_rand [1] 0.5973648 Please help me out understanding this... 2. Our goal is to link with an vectorized Random number generator library for our multi-core architecture. So is it enough if we define user_unif_rand function alone and will it take care of all distributions? Thanks in Anticipation, R. Subramanian [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
[R] Random number generation
Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, -Max __ R-help@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.
Re: [R] Random number generation
on 07/31/2008 12:24 PM Max wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, MAT - matrix(runif(1 * 6), 1, 6) str(MAT) num [1:1, 1:6] 0.753 0.600 0.806 0.713 0.796 ... head(MAT, 10) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.75343430 0.4993896 0.68554749 0.01924549 0.90579982 0.99606191 [2,] 0.59957219 0.4075650 0.57851744 0.97208426 0.32137505 0.02089689 [3,] 0.80567935 0.5746030 0.16520072 0.92615138 0.01628994 0.90075333 [4,] 0.71270574 0.3252210 0.53765089 0.58930899 0.03053356 0.23282879 [5,] 0.79603691 0.5591622 0.97308348 0.52744458 0.76403708 0.22268021 [6,] 0.49624259 0.5106604 0.06687444 0.48659150 0.29803454 0.91760758 [7,] 0.32921909 0.7784539 0.20468873 0.86730697 0.42581735 0.59344279 [8,] 0.93646405 0.4819996 0.79033546 0.68441917 0.28566573 0.97244395 [9,] 0.02964297 0.5489500 0.64355067 0.87131530 0.58505804 0.06972828 [10,] 0.55956266 0.8376349 0.11850374 0.37687892 0.71220844 0.97784727 The first argument to runif() is how many random deviates you want to generate. If you need to be able to reproduce the exact sequence again in the future, see ?set.seed. HTH, Marc Schwartz __ R-help@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.
Re: [R] Random number generation
Marc, this is very handy. My next question is, do you know a quick and easy way to transfer all of the output to a txt file? (or .xls)? Thanks, -Max Marc Schwartz explained on 07/31/2008 : on 07/31/2008 12:24 PM Max wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, MAT - matrix(runif(1 * 6), 1, 6) str(MAT) num [1:1, 1:6] 0.753 0.600 0.806 0.713 0.796 ... head(MAT, 10) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.75343430 0.4993896 0.68554749 0.01924549 0.90579982 0.99606191 [2,] 0.59957219 0.4075650 0.57851744 0.97208426 0.32137505 0.02089689 [3,] 0.80567935 0.5746030 0.16520072 0.92615138 0.01628994 0.90075333 [4,] 0.71270574 0.3252210 0.53765089 0.58930899 0.03053356 0.23282879 [5,] 0.79603691 0.5591622 0.97308348 0.52744458 0.76403708 0.22268021 [6,] 0.49624259 0.5106604 0.06687444 0.48659150 0.29803454 0.91760758 [7,] 0.32921909 0.7784539 0.20468873 0.86730697 0.42581735 0.59344279 [8,] 0.93646405 0.4819996 0.79033546 0.68441917 0.28566573 0.97244395 [9,] 0.02964297 0.5489500 0.64355067 0.87131530 0.58505804 0.06972828 [10,] 0.55956266 0.8376349 0.11850374 0.37687892 0.71220844 0.97784727 The first argument to runif() is how many random deviates you want to generate. If you need to be able to reproduce the exact sequence again in the future, see ?set.seed. HTH, Marc Schwartz __ R-help@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. __ R-help@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.
Re: [R] Random number generation
Hi Max, See ?write.table. Perhaps: MAT - matrix(runif(1 * 6), 1, 6) # TXT format write.table(MAT, C:/yourmatrix.txt,col.names=FALSE,row.names=FALSE,quote=FALSE) # XLS format write.table(MAT, C:/yourmatrix.xls,col.names=FALSE,row.names=FALSE,quote=FALSE,sep=\t) HTH, Jorge On Thu, Jul 31, 2008 at 1:53 PM, Max [EMAIL PROTECTED] wrote: Marc, this is very handy. My next question is, do you know a quick and easy way to transfer all of the output to a txt file? (or .xls)? Thanks, -Max Marc Schwartz explained on 07/31/2008 : on 07/31/2008 12:24 PM Max wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, MAT - matrix(runif(1 * 6), 1, 6) str(MAT) num [1:1, 1:6] 0.753 0.600 0.806 0.713 0.796 ... head(MAT, 10) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.75343430 0.4993896 0.68554749 0.01924549 0.90579982 0.99606191 [2,] 0.59957219 0.4075650 0.57851744 0.97208426 0.32137505 0.02089689 [3,] 0.80567935 0.5746030 0.16520072 0.92615138 0.01628994 0.90075333 [4,] 0.71270574 0.3252210 0.53765089 0.58930899 0.03053356 0.23282879 [5,] 0.79603691 0.5591622 0.97308348 0.52744458 0.76403708 0.22268021 [6,] 0.49624259 0.5106604 0.06687444 0.48659150 0.29803454 0.91760758 [7,] 0.32921909 0.7784539 0.20468873 0.86730697 0.42581735 0.59344279 [8,] 0.93646405 0.4819996 0.79033546 0.68441917 0.28566573 0.97244395 [9,] 0.02964297 0.5489500 0.64355067 0.87131530 0.58505804 0.06972828 [10,] 0.55956266 0.8376349 0.11850374 0.37687892 0.71220844 0.97784727 The first argument to runif() is how many random deviates you want to generate. If you need to be able to reproduce the exact sequence again in the future, see ?set.seed. HTH, Marc Schwartz __ R-help@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. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Random number generation
?write.table could help PF 2008/7/31 Max [EMAIL PROTECTED]: Marc, this is very handy. My next question is, do you know a quick and easy way to transfer all of the output to a txt file? (or .xls)? Thanks, -Max Marc Schwartz explained on 07/31/2008 : on 07/31/2008 12:24 PM Max wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, MAT - matrix(runif(1 * 6), 1, 6) str(MAT) num [1:1, 1:6] 0.753 0.600 0.806 0.713 0.796 ... head(MAT, 10) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.75343430 0.4993896 0.68554749 0.01924549 0.90579982 0.99606191 [2,] 0.59957219 0.4075650 0.57851744 0.97208426 0.32137505 0.02089689 [3,] 0.80567935 0.5746030 0.16520072 0.92615138 0.01628994 0.90075333 [4,] 0.71270574 0.3252210 0.53765089 0.58930899 0.03053356 0.23282879 [5,] 0.79603691 0.5591622 0.97308348 0.52744458 0.76403708 0.22268021 [6,] 0.49624259 0.5106604 0.06687444 0.48659150 0.29803454 0.91760758 [7,] 0.32921909 0.7784539 0.20468873 0.86730697 0.42581735 0.59344279 [8,] 0.93646405 0.4819996 0.79033546 0.68441917 0.28566573 0.97244395 [9,] 0.02964297 0.5489500 0.64355067 0.87131530 0.58505804 0.06972828 [10,] 0.55956266 0.8376349 0.11850374 0.37687892 0.71220844 0.97784727 The first argument to runif() is how many random deviates you want to generate. If you need to be able to reproduce the exact sequence again in the future, see ?set.seed. HTH, Marc Schwartz __ R-help@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. __ R-help@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. __ R-help@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.
Re: [R] Random number generation
m - runif(6) mm - matrix(m, ncol=6) HTH Dave On Thu, Jul 31, 2008 at 6:24 PM, Max [EMAIL PROTECTED] wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, -Max __ R-help@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. -- David Barron Said Business School Park End Street Oxford OX1 1HP 01865 288906 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Random number generation
This is perfect, thanks! :) Jorge Ivan Velez pretended : Hi Max, See ?write.table. Perhaps: MAT - matrix(runif(1 * 6), 1, 6) # TXT format write.table(MAT, C:/yourmatrix.txt,col.names=FALSE,row.names=FALSE,quote=FALSE) # XLS format write.table(MAT, C:/yourmatrix.xls,col.names=FALSE,row.names=FALSE,quote=FALSE,sep=\t) HTH, Jorge On Thu, Jul 31, 2008 at 1:53 PM, Max [EMAIL PROTECTED] wrote: Marc, this is very handy. My next question is, do you know a quick and easy way to transfer all of the output to a txt file? (or .xls)? Thanks, -Max Marc Schwartz explained on 07/31/2008 : on 07/31/2008 12:24 PM Max wrote: Hi Everyone, I did a quick search of the list and it looks like this may not have been asked before... I'm trying to generate a matrix of random numbers between 0 and 1, with 6 columns, 1 rows. About all I know is that runif(1) gives me the random number I'm looking for. Any help would be great! thanks, MAT - matrix(runif(1 * 6), 1, 6) str(MAT) num [1:1, 1:6] 0.753 0.600 0.806 0.713 0.796 ... head(MAT, 10) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.75343430 0.4993896 0.68554749 0.01924549 0.90579982 0.99606191 [2,] 0.59957219 0.4075650 0.57851744 0.97208426 0.32137505 0.02089689 [3,] 0.80567935 0.5746030 0.16520072 0.92615138 0.01628994 0.90075333 [4,] 0.71270574 0.3252210 0.53765089 0.58930899 0.03053356 0.23282879 [5,] 0.79603691 0.5591622 0.97308348 0.52744458 0.76403708 0.22268021 [6,] 0.49624259 0.5106604 0.06687444 0.48659150 0.29803454 0.91760758 [7,] 0.32921909 0.7784539 0.20468873 0.86730697 0.42581735 0.59344279 [8,] 0.93646405 0.4819996 0.79033546 0.68441917 0.28566573 0.97244395 [9,] 0.02964297 0.5489500 0.64355067 0.87131530 0.58505804 0.06972828 [10,] 0.55956266 0.8376349 0.11850374 0.37687892 0.71220844 0.97784727 The first argument to runif() is how many random deviates you want to generate. If you need to be able to reproduce the exact sequence again in the future, see ?set.seed. HTH, Marc Schwartz __ R-help@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. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Random number generation
On Behalf Of Esmail Bonakdarian wrote: ... What I read doesn't seem to be incorrect however (it may even have been an archived message here), the *language* itself does not seem to support block *comments*. Using conditional constructs, or an IDE/editor to achieve similar results is a work around - but not the same. I don't mean to nitpick, but as a computer scientist I see this as different :-) For those using NEdit, the R block comment macro: replace_in_selection(^,#,regex) and the corresponding R block uncomment macro: replace_in_selection(^#,,regex) are both trivial. Just highlight the block and detonate the macro. Jim __ R-help@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.
Re: [R] Random number generation
Greg Snow wrote: -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Esmail Bonakdarian Sent: Sunday, May 11, 2008 7:25 AM To: Prof Brian Ripley Cc: [EMAIL PROTECTED] Subject: Re: [R] Random number generation [snip] What I read doesn't seem to be incorrect however (it may even have been an archived message here), the *language* itself does not seem to support block *comments*. Using conditional constructs, or an IDE/editor to achieve similar results is a work around - but not the same. I don't mean to nitpick, but as a computer scientist I see this as different :-) I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor Hi there, Yes, while that may be true, many other languages that don't use an explicit pre-processor mechanism (though internally will go through a similar process) do support block comments. It's a moot point anyway, since when you compile a C program the preprocessor also runs (just like the linker etc) unless you specifically ask for something differently via command line switches. As far as the programmer is concerned the semantics are those of a language that provides block comments with the simple use of /* */ , and not dependent on if-statements or editor/IDE tricks. Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option. However as an additional work around My use then may be not typical since I am trying to write scripts in R and only use R directly to try out functions/language constructs. you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Too much work, call me old school, but I like the computer do work for me, rather than the other way around :-) Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. I couldn't possibly offer an opinion on that .. I'll happily defer to you and the other experts here for this. Cheers, Esmail __ R-help@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.
Re: [R] Random number generation
Esmail Bonakdarian: you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Too much work, call me old school, but I like the computer do work for me, rather than the other way around Most editors (and every editor worth using) supports block-commenting via a keyboard shortcut. For instance, in KDE-based editors, such as Kwrite, Kate and RKward (the latter is a KDE-based GUI for R), you can just select the lines you want to comment and press 'Ctrl + D' to comment them, or 'Shift + Ctrl + D' to uncomment them. -- Karl Ove Hufthammer __ R-help@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.
Re: [R] Random number generation
-Original Message- From: Esmail Bonakdarian [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 13, 2008 8:13 AM To: Greg Snow Cc: Prof Brian Ripley; [EMAIL PROTECTED] Subject: Re: [R] Random number generation Greg Snow wrote: -Original Message- [snip] you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Too much work, call me old school, but I like the computer do work for me, rather than the other way around :-) So, have the computer do the work. Here is a simple function that tells the computer to do the work: cppsource - function(file) { tmp - paste(tempfile(), '.R', sep='') system(paste('cpp',file,tmp)) source(tmp) unlink(tmp) } Now, instead of doing source('myfile.R') do cppsource('myfile.R') and it will automatically do the preproccing to remove the c-style comments (assuming that cpp is on your path, etc.). If typing the extra 3 characters is too much work, then just name it something else. Or if you just want to strip the comments in R and don't want to use the c preprocessor then the function: cssource - function(file) { tmp - readLines(file) tmp2 - paste(tmp, collapse='\n') tmp3 - gsub('(?s)/\\*.*?\\*/','',tmp2,perl=TRUE) source(textConnection(tmp3)) } Will do that (it will not deal with nested comments or some other special cases, but both the above functions worked for my quick test case). Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. I couldn't possibly offer an opinion on that .. I'll happily defer to you and the other experts here for this. I am not really an expert on this. I just remember being bitten when writing C and trying to comment out a section of code that already had a comment in it. I would rather have the R core team spending their time on their current priorities than even get involved in a discussion of whether block comments should be able to be nested or not. You now have at least 4 possible work arounds, hopefully one of them is satifactory, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 __ R-help@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.
Re: [R] Random number generation
On 5/13/2008 1:38 PM, Greg Snow wrote: -Original Message- From: Esmail Bonakdarian [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 13, 2008 8:13 AM To: Greg Snow Cc: Prof Brian Ripley; [EMAIL PROTECTED] Subject: Re: [R] Random number generation Greg Snow wrote: -Original Message- [snip] you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Too much work, call me old school, but I like the computer do work for me, rather than the other way around :-) So, have the computer do the work. Here is a simple function that tells the computer to do the work: cppsource - function(file) { tmp - paste(tempfile(), '.R', sep='') system(paste('cpp',file,tmp)) source(tmp) unlink(tmp) } Now, instead of doing source('myfile.R') do cppsource('myfile.R') and it will automatically do the preproccing to remove the c-style comments (assuming that cpp is on your path, etc.). If typing the extra 3 characters is too much work, then just name it something else. Or if you just want to strip the comments in R and don't want to use the c preprocessor then the function: cssource - function(file) { tmp - readLines(file) tmp2 - paste(tmp, collapse='\n') tmp3 - gsub('(?s)/\\*.*?\\*/','',tmp2,perl=TRUE) source(textConnection(tmp3)) } Will do that (it will not deal with nested comments or some other special cases, but both the above functions worked for my quick test case). One problem with this idea is that it makes the very helpful syntax error messages from source() less useful: cppsource('test.R') Error in source(tmp) : c:\temp\Rtmpmyoc5a\file678418be.R: unexpected symbol at 8: 9: if (y 1:2 cat Duncan Murdoch Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. I couldn't possibly offer an opinion on that .. I'll happily defer to you and the other experts here for this. I am not really an expert on this. I just remember being bitten when writing C and trying to comment out a section of code that already had a comment in it. I would rather have the R core team spending their time on their current priorities than even get involved in a discussion of whether block comments should be able to be nested or not. You now have at least 4 possible work arounds, hopefully one of them is satifactory, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 __ R-help@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. __ R-help@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.
Re: [R] Random number generation
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Esmail Bonakdarian Sent: Sunday, May 11, 2008 7:25 AM To: Prof Brian Ripley Cc: [EMAIL PROTECTED] Subject: Re: [R] Random number generation [snip] What I read doesn't seem to be incorrect however (it may even have been an archived message here), the *language* itself does not seem to support block *comments*. Using conditional constructs, or an IDE/editor to achieve similar results is a work around - but not the same. I don't mean to nitpick, but as a computer scientist I see this as different :-) I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor replaces the comments with a single space and the compilor does not even see them. Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option. However as an additional work around you could always run your R scripts through the C preproccessor and have it strip the block comments for you. Given the complexity of implementing block commenting (even deciding on the syntax) and the ease of current work arounds, the cost benefit ratio probably puts this very near the bottom of the priority list. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 __ R-help@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.
Re: [R] Random number generation
Dennis, I assume that there is a set.seed() somewhere in your script, possibly in something you source()d (hopefully not in anything library()d). Have you tried successively removing/commenting parts of the script before the sample() command until the problem goes away? That way you should be able to pinpoint the offending script command. Good luck, Stephan Dennis Fisher schrieb: Colleagues, I have encountered behavior of random number generation that eludes me. I generate a random integer in a particular range using the following code: sample(1000:, size=1) This code exists within a script that starts with the command: remove(list=ls()) Each time that I run the script, it yields the same random number: 6420. I thought that the problem might result from deleting the random seed. However, list=ls() does not include .RandomSeed. To my surprise, I can't replicate the problem with a 2-line script: remove(list-ls()) sample(1000:, size=1) Also, the same problem occurs if I use runif instead of sample. Thoughts? Dennis Dennis Fisher MD P (The P Less Than Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Random number generation
Stephan Kolassa wrote: Have you tried successively removing/commenting parts of the script before the sample() command until the problem goes away? That way you should be able to pinpoint the offending script command. Hi, This brings up a question I have .. is there a way to do *block* comments with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment more than just one line at a time. From what I have read this is not possible in R (at least not easily), but I am eager for someone to contradict me :-) Thanks, Esmail __ R-help@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.
Re: [R] Random number generation
On Sun, 11 May 2008, Esmail Bonakdarian wrote: Stephan Kolassa wrote: Have you tried successively removing/commenting parts of the script before the sample() command until the problem goes away? That way you should be able to pinpoint the offending script command. Hi, This brings up a question I have .. is there a way to do *block* comments with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment more than just one line at a time. From what I have read this is not possible in R (at least not easily), but I am eager for someone to contradict me :-) if(FALSE) { ... } Any good editor can do block commenting, e.g. ESS. You didn't tell us what you read, but I have never seen this in a reputable source. Thanks, Esmail -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@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.
Re: [R] Random number generation
Hello there, Prof Brian Ripley wrote: On Sun, 11 May 2008, Esmail Bonakdarian wrote: Stephan Kolassa wrote: Have you tried successively removing/commenting parts of the script before the sample() command until the problem goes away? That way you should be able to pinpoint the offending script command. Hi, This brings up a question I have .. is there a way to do *block* comments with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment more than just one line at a time. From what I have read this is not possible in R (at least not easily), but I am eager for someone to contradict me :-) if(FALSE) { ... } Any good editor can do block commenting, e.g. ESS. You didn't tell us what you read, but I have never seen this in a reputable source. I don't remember the source as I was reading widely all over the place trying to get up to speed with R in a hurry (having found this group was one of the best sources). What I read doesn't seem to be incorrect however (it may even have been an archived message here), the *language* itself does not seem to support block *comments*. Using conditional constructs, or an IDE/editor to achieve similar results is a work around - but not the same. I don't mean to nitpick, but as a computer scientist I see this as different :-) I'll have to look at ESS though. Thanks, Esmail ps: Ah, I seem Meta-; works as a toggle in emacs/ESS .. thanks for encouraging me to look at that some more. __ R-help@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.
[R] Random number generation
Colleagues, I have encountered behavior of random number generation that eludes me. I generate a random integer in a particular range using the following code: sample(1000:, size=1) This code exists within a script that starts with the command: remove(list=ls()) Each time that I run the script, it yields the same random number: 6420. I thought that the problem might result from deleting the random seed. However, list=ls() does not include .RandomSeed. To my surprise, I can't replicate the problem with a 2-line script: remove(list-ls()) sample(1000:, size=1) Also, the same problem occurs if I use runif instead of sample. Thoughts? Dennis Dennis Fisher MD P (The P Less Than Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]] __ R-help@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.