help I need matlab code to to change randn instruction to rand and limit its value between 0 and 1

조회 수: 6 (최근 30일)
help I need matlab code to to change randn instruction to rand and limit its value between 0 and 1

답변 (2개)

Star Strider
Star Strider 2014년 11월 20일
If you want normally distributed random numbers on the interval [0,1], this comes close:
randnlim = @(r,c) (0.1*randn(r,c) + 0.5);
  댓글 수: 4
Star Strider
Star Strider 2014년 11월 20일
Yes it does!
Try this to see the distribution:
randnlim = @(r,c) (0.1*randn(r,c) + 0.5);
hist(randnlim(1,1E+6),100)
The normal distribution has infinite support — normally-distributed variables are by definition on the interval (-Inf,+Inf). This function limits the probabilities to fall within [0,1] a significant part of the time. A truly normally-distributed random variable will never be constrained within finite limits.

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


Image Analyst
Image Analyst 2014년 11월 20일
randn() creates numbers outside the range 0-1. You can either remove them
r = randn(1, 100);
r(r<0) = [];
r(r>1) = [];
or clip them
r(r<0) = 0;
r(r>1) = 1;
Which do you want? Does this answer your question?
  댓글 수: 2
maha ismail
maha ismail 2014년 11월 20일
no dear I need to limt the value of randn between 0 and 1 just like rand instruction
Image Analyst
Image Analyst 2014년 11월 20일
Yes dear. The code will not allow any values of r outside the [0,1] range yet it will still have the normal distribution, well as much as you can without allowing values between minus and plus infinity.
If you want to redefine randn() so that it has that name but does what my code does, then you can do that. You can override functions to make them do your own custom stuff. But you'd have to rename the original randn() first otherwise it would be recursive.
If you just want a flat distribution like rand(), then use rand().

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

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by