i need to generate a random number out of a range, i did it inside with r=-1+(2).*rand(1,1) %my interval is (-1;1)
how can i do to get a random number that is not in this interval?? i mean, a number which satisfy r<-1 U 1<r

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 21일
편집: Azzi Abdelmalek 2014년 1월 21일
a=rand
out=1/a*sign(a-0.5)
%or for a vector
n=10;
a=rand(1,n);out=1./a.*sign(a-0.5)

댓글 수: 2

Amit
Amit 2014년 1월 21일
편집: Amit 2014년 1월 21일
I think this technique would be biased. The negative random number would be always bigger while the positive ones will always be smaller.
You are right, if we use rand twice, it will be different
n=10;
out=1./rand(1,n).*sign(rand(1,n)-0.5)

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

추가 답변 (1개)

Amit
Amit 2014년 1월 21일
A = [1/rand -1/rand];
r = A(randi(2));
(or)
r = [1/rand -1/rand]*(randperm(2,2) -1)';
Either will generate a random number from r<-1 U 1<r

카테고리

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

질문:

2014년 1월 21일

댓글:

2014년 1월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by