3.4 Generate uniform distributed random numbers

3.4.1 How to generate 5 uniform distributed random numbers from 0 to 1?
3.4.2 How to generate 5 random numbers from a to b?
3.4.3 How to generate MATRIX of random numbers from a to b?

3.4.1 How to generate 5 uniform distributed random numbers from 0 to 1?

Mathematica

SeedRandom[1]; 
Table[RandomVariate[ 
   UniformDistribution[{0,1}]],{5}]
 

{0.817389, 
0.11142, 
0.789526, 
0.187803, 
0.241361}
 

 

Matlab

rand(5,1)
 

   0.814723686393179 
   0.905791937075619 
   0.126986816293506 
   0.913375856139019 
   0.632359246225410
 

 

Fortran

program t3implicit none 
real :: x(5) 
 
 CALL RANDOM_SEED() 
 CALL random_number(x) 
 print *,x 
         
end program
 

compile and run

$ gfortran -std=f95 -Wextra -Wall -pedantic -funroll-loops 
  -ftree-vectorize -march=native  -Wsurprising -Wconversion 
  t3.f90  /usr/lib/liblapack.a /usr/lib/libblas.a 
$ ./a.exe 
  0.99755955      0.56682467      0.96591532      0.74792767      0.36739087
 

3.4.2 How to generate 5 random numbers from a to b?

Generate uniform numbers from a to b, say a=-2 and b=5

Mathematica

SeedRandom[1]; 
Table[RandomVariate[ 
    UniformDistribution[{-2,5}]],{5}]
 

         {3.72173, 
         -1.22006, 
          3.52668, 
         -0.685378, 
         -0.310473}
 

 

Matlab

-2 + (5+2)*rand(5,1)
 

  -1.317217165004133 
  -0.050512467930661 
   1.828170634434887 
   4.702547848040084 
   4.754219746394936
 

 

Fortran

program t3_2implicit none 
integer ::i 
real, parameter :: a=-1.0,b=1.0 
real :: x(5) 
 
 CALL RANDOM_SEED() 
 DO i=1,2 
    CALL random_number(x) 
    x = a+(b-a)*x 
    print *,x 
 END DO 
         
end program
 

compile and run

$ gfortran -std=f95 -Wextra -Wall -pedantic -funroll-loops 
  -ftree-vectorize -march=native  -Wsurprising -Wconversion 
  t3_2.f90  /usr/lib/liblapack.a /usr/lib/libblas.a 
$ ./a.exe 
  0.99511909      0.13364935      0.93183064      0.49585533     -0.26521826 
 -3.87262106E-02 -0.85249150     -0.98928964     -0.30583751     -0.31551242 
$
 

3.4.3 How to generate MATRIX of random numbers from a to b?

Let \(a=-2\) and \(b=5\), matrix of size \(5\) by \(5\)

Mathematica

SeedRandom[1]; 
Table[RandomVariate[ 
   UniformDistribution[{-2,5}]],{5},{5}]
 
{{3.72173,-1.22006,3.52668,-0.685378,-0.310473}, 
 {-1.53983,1.79573,-0.381918,0.772043,2.90332}, 
 {-0.517218,3.2406,0.959955,-0.267537,4.8402}, 
 {3.77614,4.47693,2.04639,0.0500882,-0.543643}, 
 {2.06332,-1.09825,0.144992,2.98408,0.734073}}
 

Matlab

-2 + (5+2)*rand(5,5)
 
ans = 
 3.7642    1.0712    1.4284   -0.0678    1.4885 
 2.8638    0.6709    1.1191    2.7579    4.7182 
 0.2197    3.3586    2.5242    2.5857    0.3827 
 4.6516    3.5664    2.9656   -0.8617    2.0969 
 -1.7589   -0.6919    3.2828   -1.1670   -0.4333
 

Fortran

program t3_3implicit none 
integer ::i 
real, parameter :: a=-1.0,b=1.0 
real :: x(5,5) 
 
 CALL RANDOM_SEED() 
 DO i=1,2 
    CALL random_number(x) 
    x = a+(b-a)*x 
    CALL write(x) 
 END DO 
         
 !--- internal functions below ------ 
     contains 
       SUBROUTINE write(A) 
       implicit none 
       REAL, DIMENSION(:,:) :: A 
       integer :: i,j 
 
       WRITE(*,*) 
       DO i = lbound(A,1), ubound(A,1) 
          WRITE(*,*) (A(i,j), j = lbound(A,2), ubound(A,2)) 
       END DO 
 
  END SUBROUTINE write 
 
end program
 

compile and run

$ gfortran -std=f95 -Wextra -Wall -pedantic -funroll-loops 
    -ftree-vectorize -march=native  -Wsurprising -Wconversion 
    t3_3.f90  /usr/lib/liblapack.a /usr/lib/libblas.a 
 
$ ./a.exe 
  0.99511909     -3.87262106E-02 -0.56409657      0.32386434      0.71138477 
  0.13364935     -0.85249150     -0.73367929     -0.96778345     -0.19742620 
  0.93183064     -0.98928964      0.80104899      0.30170965     -0.58625138 
  0.49585533     -0.30583751     -0.22646809      0.29281759      0.93707883 
 -0.26521826     -0.31551242     -0.10903549     -0.35402548      0.19679904 
 
  0.34596145      0.21138644      0.22462976      0.31809497      0.45771694 
 -8.62354040E-02  0.43809581      0.95732045      0.10801017     -0.19508958 
 -0.33996975      0.79466915      0.99828446      0.95552015      0.85725522 
 -0.79923415      0.31645823     -0.48640406      0.80384660     -0.70432973 
  0.51090658     -0.69856644      0.10173070      0.31584930      0.34905851