Generate a random number in a certain range in MATLAB

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

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

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

    You can also use:

    round(mod(rand.*max,max-1))+min
    
    0 讨论(0)
  • 2020-11-28 10:52

    Best solution is randint , but this function produce integer numbers.

    You can use rand with rounding function

      r = round(a + (b-a).*rand(m,n));
    

    This produces Real random number between a and b , size of output matrix is m*n

    0 讨论(0)
  • 2020-11-28 10:53

    ocw.mit.edu is a great resource that has helped me a bunch. randi is the best option, but if your into number fun try using the floor function with rand to get what you want.

    I drew a number line and came up with

    floor(rand*8) + 13
    
    0 讨论(0)
  • 2020-11-28 10:55
    r = 13 + 7.*rand(100,1);
    

    Where 100,1 is the size of the desidered vector

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

    if you are looking to generate all the number within a specific rang randomly then you can try

    r = randi([a b],1,d)
    

    a = start point

    b = end point

    d = how many number you want to generate but keep in mind that d should be less than or equal to b-a

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

    http://www.mathworks.com/help/techdoc/ref/rand.html

    n = 13 + (rand(1) * 7);
    
    0 讨论(0)
提交回复
热议问题