2.7 Generate a random 2D matrix from uniform (0 to 1) and from normal distributions

Mathematica

(*from uniform over 0,1*) 
mat = RandomReal[ 
       UniformDistribution[{0,1}],{3,4}]
 

 {{0.100843,0.356115,0.700317,0.657852}, 
  {0.238019,0.59598,0.523526,0.576362}, 
  {0.339828,0.32922,0.0632487,0.815892}}
 

(*from normal, zero mean, 1 std*) 
 
mat=RandomReal[ 
   NormalDistribution[0,1],{3,4}]
 

{{-0.226424,1.19553,0.601433,1.28032}, 
{1.66596,0.176225,-0.619644,0.371884}, 
{0.895414,-0.394081,0.153507,-1.74643}}
 

 

Matlab

mat=rand(3,4)
 

mat = 
 0.6787    0.3922    0.7060    0.0462 
 0.7577    0.6555    0.0318    0.0971 
 0.7431    0.1712    0.2769    0.8235
 

mat=randn(3,4)
 

mat = 
 0.3252   -1.7115    0.3192   -0.0301 
-0.7549   -0.1022    0.3129   -0.1649 
 1.3703   -0.2414   -0.8649    0.6277
 

 

Maple

A:=ArrayTools:-RandomArray(3,4, 
              distribution = uniform); 
 
#just to round to 2 decimal points 
parse~(sprintf~("%0.2f",A));
 

[[0.93,0.66,0.92,0.80], 
 [0.85,0.96,0.42,0.49], 
 [0.04,0.79,0.14,0.96] 
]
 

 

Or

interface(displayprecision=5): 
A:=ArrayTools:-RandomArray(3,4, 
             distribution = uniform);
 

\[ \left [ {\begin {array}{cccc} 0.970592781760615697& 0.957506835434297598& 0.0975404049994095246& 0.126986816293506055\\ \noalign {\medskip } 0.157613081677548283& 0.546881519204983846& 0.632359246225409510& 0.905791937075619225\\ \noalign {\medskip } 0.964888535199276531& 0.278498218867048397& 0.913375856139019393& 0.814723686393178936\end {array}} \right ] \]

Fortran

program t5
  implicit none 
  INTEGER(4) ::i=0,j 
  REAL ::A(3,3),mean,sd,pi,temp 
 
  CALL RANDOM_SEED(i) 
  CALL RANDOM_NUMBER(A) 
 
  print *, 'from uniform distribution' 
  do i=LBOUND(A, 1),UBOUND(A, 1) 
     print *,A(i,:) 
  end do 
 
  !this block below uses the logic as explained in 
  !http://rosettacode.org/wiki/Random_numbers#Fortran 
  ! 
  mean=0.0 
  sd=1.0 
  pi = 4.0*ATAN(1.0) 
 
  ! Now convert to normal distribution 
    DO i = 1, SIZE(A,1)-1, 2 
      DO j = 1, SIZE(A,2)-1, 2 
         temp = sd * SQRT(-2.0*LOG(A(i,j))) * COS(2*pi*A(i+1,j+1)) + mean 
         A(i+1,j+1) = sd* SQRT(-2.0*LOG(A(i,j)))*SIN(2*pi*A(i+1,j+1))+mean 
         A(i,j) = temp 
      END DO 
   END DO 
 
 print *, 'from normal distribution' 
 do i=LBOUND(A, 1),UBOUND(A, 1) 
      print *,A(i,:) 
 end do 
 
end program t5
>gfortran -Wall -std=f2008 t5.f90 
>./a.out 
 from uniform distribution 
  0.99755955      0.74792767      7.37542510E-02 
  0.56682467      0.36739087      5.35517931E-03 
  0.96591532      0.48063689      0.34708124 
 
 from normal distribution 
 -4.70122509E-02  0.74792767      7.37542510E-02 
  0.56682467      5.17370142E-02  5.35517931E-03 
  0.96591532      0.48063689      0.34708124
 

Did not find a build-in support for random numbers from normal distribution, need to look more.