how can i change the interval of rand?
Show older comments
I need to change the interval of the rand function [0,1] a [-0.4,0.4] the fact is that I would not use a + (ba) * rand (193.8) because it distorts my data for the rest of my work because I need the sum of my line is equal to a precise number
6 Comments
John D'Errico
on 4 Apr 2014
Sigh. You want the impossible. Try magic. Or perhaps prayer is the answer.
vachelard
on 4 Apr 2014
Joseph Cheng
on 4 Apr 2014
how will rand get you your precise number?
vachelard
on 4 Apr 2014
dpb
on 4 Apr 2014
You can always normalize after the scaling if that's a viable solution. The result isn't a truly random number of course, but the values will be distributed within the restricted range with the same pdf.
vachelard
on 6 Apr 2014
Answers (1)
Roger Stafford
on 5 Apr 2014
I will first respond to the code you wrote which normalizes the sum of your eight-element rows to one. This code can easily be changed to have all elements lying between -0.4 and 0.4, provided you can adjust to the idea of writing "-0.4+0.8*rand" instead of "rand" or writing a function to do that. I see no rational reason for avoiding the former method of random number generation.
However, even making that adjustment, there is a possible flaw in the results as to the probability density you obtain with this method. This is easiest to see using only two 'rand' variables normalized to sum to one. In a two-dimensional plot of such a set of random points you will find that they are heavily concentrated in the central part of the straight line they lie on and the density falls off to zero at each end. It was to avoid this biased distribution that I wrote 'randfixedsum'. It gives a uniform distribution volume/area/length-wise to the generated set of values. It also allows you to set the arbitrary limiting values a and b for the variables and the sum s which they must have. In your case it would be a = -0.4, b = 0.4, and s = 0. You will find 'randfixedsum' at:
http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
5 Comments
Image Analyst
on 5 Apr 2014
Roger, what's the link to that Answer you gave a month or so ago where you gave a really nice illustration (plot) of how the 2D rand operation was not uniformly spaced out?
Roger Stafford
on 5 Apr 2014
Edited: Image Analyst
on 5 Apr 2014
Was it this one:
Image Analyst
on 5 Apr 2014
Edited: Image Analyst
on 5 Apr 2014
I found it. Actually it was from John D'Errico, not you, though I'd be interested in what you think. http://www.mathworks.com/matlabcentral/answers/121609#answer_128444
Roger Stafford
on 5 Apr 2014
Yes, that is a good demonstration of the point I was making above. John's plot shows how the distribution of two 'rand' quantities, whose sum has been normalized, is concentrated at the center of the diagonal line and drops to zero at the two ends.
vachelard
on 5 Apr 2014
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!