How do I generate a random number between two numbers,?
3.081 views (last 30 days)
how i get random number between two numbers , like i want random number between 20-150 like this , random, i know the max number and the minimum number and i want to matlab gave me a random number between the max and the minimum
Image Analyst on 18 Feb 2018
The second example for rand() says this:
"In general, you can generate N random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(N,1)."
With a=20 and b=150, you get what you want.
r = 20 + (150-20) .* rand(N,1)
More Answers (4)
Nisha Bharti on 12 Jan 2022
To generate a random number between [a,b]:
r = a + (b-a)*rand();
To generate N numbers:
r = a + (b-a)*rand(1,N);
Hope that helps!
Enis Hidisoglu on 29 Jan 2021
Hello my friend,
you can use below codes to generate random number between two number which you want; for example between 20-150,
trial = [20:1:150];
r = randperm(lenght(trial));
trial = trial(r)
I think this will be useful for you...
Tamas Kis on 25 Jul 2021
Generates random numbers in a range, allows for type specification as well as the desired output size: