MATLAB Tutorials Class3 : How to Generate random number in MATLAB

MATLAB Tutorials Class3: How to Generate a

 random number in MATLAB

Hai... We addressed the creation of an array and matrix in my previous post. We will see how to generate a random number in MATLAB here. 

1. How to generate uniformly distributed random numbers in MATLAB?

Syntax is 

                    rand_val_creation=rand()

The uniformly distributed number 0 to 1 range is generated by the rand function. We can create a random matrix, just like the creation of zeros and one's matrix. 

Case 1:if we give syntax like 'rand' means it creates 1 x 1 rand matrix range between 0 to 1.

>> rand_no_creation=rand

rand_no_creation =

    0.8147

Case 2: If we mention a number, that number is considered to be a row and column count.

>> rand_no_creation=rand(4)

rand_no_creation =

    0.9058    0.0975    0.9649    0.4854
    0.1270    0.2785    0.1576    0.8003
    0.9134    0.5469    0.9706    0.1419
    0.6324    0.9575    0.9572    0.4218

Case 3: if suppose we need a specified number of row and column random matrix means we can mention the number of row and column as given below

>> rand_no_creation=rand(4,6)

rand_no_creation =

    0.9157    0.0357    0.7577    0.1712    0.0462    0.3171
    0.7922    0.8491    0.7431    0.7060    0.0971    0.9502
    0.9595    0.9340    0.3922    0.0318    0.8235    0.0344
    0.6557    0.6787    0.6555    0.2769    0.6948    0.4387

How to generate a specific range random matrix in  MATLAB

Case 4: we can create a specific range random matrix in MATLAB as given below

starting_no=1;
ending_no=100;
r = starting_no + (ending_no-starting_no).*rand(1)

r =

   78.1376

r = starting_no + (ending_no-starting_no).*rand(5)
r =

   93.4671   34.3751   17.3992   75.0670   16.0854
   13.8607   17.0560   60.5962   45.6036   82.7559
   57.3135   79.6342   27.0342    9.2983   54.2959
   47.4697   31.8103   65.7538   23.6687   99.6173
    2.1783   53.3248   69.2322   91.4204    8.7394

r = starting_no + (ending_no-starting_no).*rand(5,3)

r =

   40.7790   24.7553   49.5955
    8.5207   42.3094   49.4360
   24.7517    5.9158   34.4342
   13.2086   90.3689   90.1053
   19.2069   94.5339   37.5554

2. How to generate Uniformly distributed pseudorandom integers in MATLAB?

Syntax is 

                    rand_val_creation=randi(maxvalue)

The uniformly distributed pseudorandom integer generates value from 1 to maxvalue. And also we can change the range  
 
Case 1: it will generate one random number from 1 to 5 

>> rand_val_creation=randi(5)

rand_val_creation =

     3
Case 2: It will generate a 5 x 5 random matrix range from 1 to 5 

>> rand_val_creation=randi(5,5)

rand_val_creation =

     2     3     5     3     5
     5     2     1     2     5
     5     2     2     5     3
     3     3     1     3     1
     4     2     2     1     2

Case3: It will generate a 5 x 3 random matrix range from 1 to 5 

>> rand_val_creation=randi(5,5,3)

rand_val_creation =

     3     2     3
     3     1     1
     2     2     2
     4     2     5
     4     3     1

Case4: It will generate a random matrix range from min to max

>> rand_val_creation=randi([5 20])

rand_val_creation =

    19

>> rand_val_creation=randi([5 20],4)

rand_val_creation =

    17    15    15    19
     6     7    12    19
     9    16    17    10
    10     6    16    16

>> rand_val_creation=randi([5 20],4,5)

rand_val_creation =

     8    12    18     8     7
     5    19    17    19    20
    16    14    14     5    16
    13    14     7    12    13

3. How to generate Normally distributed random numbers in MATLAB?

Syntax is 

                    rand_val_creation=randn()

The normally distributed number is generated by the randn function.

Exanples:

>> rand_val_creation=randn

rand_val_creation =

   -1.0722

>> rand_val_creation=randn(4,3)

rand_val_creation =

    0.9610   -0.1977    1.3790
    0.1240   -1.2078   -1.0582
    1.4367    2.9080   -0.4686
   -1.9609    0.8252   -0.2725

>> rand_val_creation=randn(4)

rand_val_creation =

    1.0984   -0.3538    0.2820    0.3502
   -0.2779   -0.8236    0.0335   -0.2991
    0.7015   -1.5771   -1.3337    0.0229
   -2.0518    0.5080    1.1275   -0.2620

How to create random 3D and 4D arrays in MATLAB?

>> rand_val_creation=randn(4,2,3)

rand_val_creation(:,:,1) =

   -0.5320   -0.7120
    1.6821   -1.1742
   -0.8757   -0.1922
   -0.4838   -0.2741


rand_val_creation(:,:,2) =

    1.5301    1.2347
   -0.2490   -0.2296
   -1.0642   -1.5062
    1.6035   -0.4446


rand_val_creation(:,:,3) =

   -0.1559    0.3919
    0.2761   -1.2507
   -0.2612   -0.9480
    0.4434   -0.7411

>> rand_val_creation=randn(4,2,4)

rand_val_creation(:,:,1) =

   -0.5078   -0.4570
   -0.3206    1.2424
    0.0125   -1.0667
   -3.0292    0.9337


rand_val_creation(:,:,2) =

    0.3503   -0.0845
   -0.0290    1.6039
    0.1825    0.0983
   -1.5651    0.0414


rand_val_creation(:,:,3) =

   -0.7342   -0.3728
   -0.0308   -0.2365
    0.2323    2.0237
    0.4264   -2.2584


rand_val_creation(:,:,4) =

    2.2294   -0.5900
    0.3376   -0.2781
    1.0001    0.4227
   -1.6642   -1.6702

Reference:


We have discussed the random number creation in Matlab so far. We will see the next topic in my next post.

Thank you so much😇
Please subscribe😊.
Share with your friends and please provide your feedback about this post🙏🙏.


Comments

Popular posts from this blog

MATLAB Tutorials class 4 : MATLAB command diag, det, trace

MATLAB Tutorials class 5: MATLAB inbuild command repmat,repelem,cat,horzcat, and vertcat

Improved Median filter MATLAB code and explanation ( without inbuilt function and with inbuilt function)