how can i change the interval of rand?

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

Sigh. You want the impossible. Try magic. Or perhaps prayer is the answer.
lool I ask you never know!
how will rand get you your precise number?
I use this code to generate my variables and they sum to 1, but they take random values between (0,1) I need they take values between (-0.4,0.4).
mat =rand(193,8);
rowsum = sum(mat,2);
mat = bsxfun(@rdivide, mat, rowsum);
I heard about the function randfixedsum but I do not manage to make it roll
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.
how can I do to normalize in this case?

Sign in to comment.

Answers (1)

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, 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?
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
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.
I try to make use of the function, but I can not do I have to enter a wrong parameter. Because this is mine to pause my own assumptions I can assume my normal yields but in this case how should I do to normalize between a certain range?

Sign in to comment.

Categories

Tags

No tags entered yet.

Asked:

on 4 Apr 2014

Commented:

on 6 Apr 2014

Community Treasure Hunt

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

Start Hunting!