[R] Random number generation

Marc Schwartz marc_schwartz at comcast.net
Thu Jul 31 19:30:49 CEST 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, 10000 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(10000 * 6), 10000, 6)

 > str(MAT)
  num [1:10000, 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



More information about the R-help mailing list