How do I generate a random number between two numbers,?
조회 수: 2,667 (최근 30일)
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 2018년 2월 18일
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)
추가 답변 (4개)
Nisha Bharti 2022년 1월 12일
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 2021년 1월 29일
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 2021년 7월 25일
Generates random numbers in a range, allows for type specification as well as the desired output size: