How can I make 1 by length x array of random numbers from -pi to pi ??

조회 수: 4 (최근 30일)
Chris
Chris 2021년 10월 31일
댓글: M.MUSBA Elhadid 2021년 11월 1일
How can I make 1 by length x array of random numbers from -pi to pi ??

채택된 답변

Jan
Jan 2021년 11월 1일
See:
help rand
% Generate values from the uniform distribution on the interval (a, b):
r = a + (b - a) .* rand(x, 1);
Now set a=-pi and b=p:
r = -pi + 2*pi * rand(x, 1);
Actually we do not solve homework questions in the forum. But here I hope you see, how useful the hint is to take a look into the documentation. For questions concerning a command, start with help() and doc(). If this does not help directly, look at the bottom in the "See also" line, which contains other similar commands.

추가 답변 (1개)

M.MUSBA Elhadid
M.MUSBA Elhadid 2021년 10월 31일
편집: M.MUSBA Elhadid 2021년 10월 31일
x = ones(1,100); a = [rand(1,length(x))-0.5]+linspace(-pi+0.5,pi-0.5,length(x));
  댓글 수: 2
John D'Errico
John D'Errico 2021년 11월 1일
편집: John D'Errico 2021년 11월 1일
I'm sorry, but do you realize this solution does not generate uniformly distributed random numbers? Worse, it requires far more effort than the true solution.
Does it work? Is it even remotely close? Perhaps they only way to prove it is to do a large simulation.
a = zeros(1000,100);
for i = 1:1000
x = ones(1,100); a(i,:) = [rand(1,length(x))-0.5]+linspace(-pi+0.5,pi-0.5,length(x));
end
hist(a(:),1000)
Does that look even remotely uniformly distributed? See the answer by Jan for a far better solution. And even worse, you are trying to do what is surely someone's homework assignment. We do not do homework assignments for students on this site.
M.MUSBA Elhadid
M.MUSBA Elhadid 2021년 11월 1일
thank you for the advice. I will check next times.

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by