how can i change the interval of rand?
5 views (last 30 days)
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
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.
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!