Generate a random number in a certain range in MATLAB

前端 未结 9 1463
误落风尘
误落风尘 2020-11-28 10:08

How can I generate a random number in MATLAB between 13 and 20?

相关标签:
9条回答
  • 2020-11-28 11:11

    If you need a floating random number between 13 and 20

    (20-13).*rand(1) + 13
    

    If you need an integer random number between 13 and 20

    floor((21-13).*rand(1) + 13)
    

    Note: Fix problem mentioned in comment "This excludes 20" by replacing 20 with 21

    0 讨论(0)
  • 2020-11-28 11:12

    If you are looking for Uniformly distributed pseudorandom integers use:

    randi([13, 20])
    
    0 讨论(0)
  • 2020-11-28 11:12

    Generate values from the uniform distribution on the interval [a, b].

          r = a + (b-a).*rand(100,1);
    
    0 讨论(0)
提交回复
热议问题