MATLAB: Create random path with known end and start point

random number generator

Hi,
I would like to create "random" paths starting from one known value and ending to another one. They should not be too extreme so I have limited that I don't accept paths who get over twice as big or half as small than starting and ending points.
I have partial solution but it doesn't work when StartPoint and EndPoint are equal as it forces all values to be equal (forcing to different values to be same results this be definition)…
Any ideas? Not need to be perfect, but I would like to generate some of these to compare case where we get from start point to end point via straight line.
PathLength=100;
NumberOfSimulations=10;
StartPoint=1000;
EndPoint=2000;
Saved=NaN*zeros(PathLength, NumberOfSimulations);
n=0;
while n~=NumberOfSimulations
r =randn(PathLength,1); %Random nromally distributed numbers
c=cumsum(r);
%Linear equation to scale values with start and end point
k=(EndPoint-StartPoint)/(c(end)-c(1));
b=StartPoint-k*c(1);
d=c*k+b;
if (max(d)>(2*max([StartPoint,EndPoint]))|| min(d)<min([StartPoint, EndPoint])/2);
%not good path
else
n=n+1;
all(:,n)=d;
end
end
figure; hold on; plot(all) ; plot(mean(all,2),'k','Linewidth',2.5);
//Edit I found one solution but it is only for same size steps. Still need help.
%%http://cnr.lwlss.net/ConstrainedRandomWalk/
figure;close all;
%Biased random walk
NumberOfSimulations=10;
n=100; % number of steps, nt increasing and n(t-1) decreasing
x0=10; %StartPoint
xtarg=20; %End point after n steps
Saved=NaN*zeros(n+1,NumberOfSimulations+2);
Saved(1,:)=x0;
Saved(n+2,:)=xtarg;
for q=1:NumberOfSimulations
unifs=rand(n+1,1);
x=x0;
for i=0:(n-1)
t =(1-(xtarg-x)/(n-i))/2;
if unifs(i+1,1)<=t
x=x-1;
else
x=x+1;
end
Saved(i+2,q)=x;
end
end
figure; hold on; plot(Saved) ; plot(mean(Saved,2),'k','Linewidth',2.5);

Best Answer

The goal is a ONE dimensional random walk?
What distribution do you want for the steps? normal? Uniform? Yes, I know that you are using randn, but is that what you WANT?
I have no idea what you mean by half as big or small. Provide a clear explanation. Is this relative to the start point? The difference from start to end? Since either of those points might be zero, or the difference zero, BE CLEAR. I can help you, but I need to know some answers first.
Edit: Since no answer, I'll make some assumptions. So normally distributed random steps in the walk.
startpoint = 0;
endpoint = 10;
% number of steps
N = 100;
% Nominal stddev for each step
s = 1;
steps = randn(1,N-1)*s;
% ensure the sum is exactly zero
steps = steps - mean(steps);
% add in a bias to each step.
steps = steps + (endpoint - startpoint)/(N-1);
walk = cumsum([startpoint,steps]);
walk(1)
ans =
0
walk(end)
ans =
10
So a random walk, with normal steps.
You talked about a max or a min step size. But really, the problem was in how you were trying to renormalize the random sample. You were trying to adjust the standard deviation, when in fact, you wanted to adjust the mean.