Question about random numbers and rejecting

Hi there, I'm new to matlab. I'm trying to make a function, where you input a,b, and c generates a random integer from a to b and the function rejects the c value. I tried
function r =randnum_reject(a,b,c)
r=randi([a,b]);
if r==c
?????
end
end
I don't know what to do after the if statement. Any help? Thanks EXAMPLE: Like say I type randnum_reject(1,10,2), and my function will output a random number from 1 to 10, but NOT 2.

댓글 수: 2

Jason
Jason 2011년 8월 15일
Like say I type randnum_reject(1,10,2), and my function will output a random number from 1 to 10, but NOT 2.
Oleg Komarov
Oleg Komarov 2011년 8월 15일
Now clear.

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

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 8월 15일

1 개 추천

function r = randnum_reject(a,b,c)
% Rudimental check
if isequal(a,b,c)
return
end
r = c;
while r == c
r = randi([a b]);
end
end
You might wanna improve input checking to avoid:
randnum_reject(0,0,0)
which will get you into an infinite loop.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

질문:

2011년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by