Problem in generation of random number

5 views (last 30 days)
hi!
i used this matlab function to generate random numbers:
%
h=randi([4 18],100,5);
how could generate random number with a maximal diffrence of 1 to 4 between two successive numbers?
for exemple:
4 7 3 5 5 9 13 14 10.....
i hope you could help me! thanks
[EDITED, added from answer section]
My question is that you understood. But i want to generate it with 5 dimension (size(h)=100*5). thanks for the answer
[EDITED, added from comment to answer section]
sorry! yes i want 100*5 array. the maximal diffrence of 4 should be between tw successive numbers. as an exemple:
%
[a1 a2 a3...;
a6 a7 a8....;
a11 a12 a13...;
....;
.....]
abs(a1-a2)<=4 and a1-a6<=4,
abs(a6-a11)<=4 and abs(2-a3)<=4 and so on
Thanks again
  2 Comments
Jan
Jan on 6 Dec 2012
What have you tried so far to solve your problem? Which difficulties appeared?
The limitation of the distance in both directions increases the complexity of this task substantially.
the cyclist
the cyclist on 6 Dec 2012
Please, please try to give a complete description all at once, and not one sentence at a time.
So, you want a 100x5 array (but you show a vector in your example). Exactly which entries count as "successive", with a maximal difference of 4? Are the 100th element (at the bottom of the first column) and the 1001st element (top of the 2nd column) successive? What about two element that are next to each other on the same row?

Sign in to comment.

Accepted Answer

José-Luis
José-Luis on 6 Dec 2012
Edited: José-Luis on 7 Dec 2012
This will do what you ask, both row and column wise. It is not the most efficient thing out there, but at least it should give you a clue on how to proceed.
numVal = 100*5;
oldVal = randi([4 18]);
%Padding
your_vals = NaN*ones(100,5);
your_vals = padarray(your_vals,[1 1],NaN);
idx_pad = reshape(1:numel(your_vals),size(your_vals));
idx_pad(:,[1 end]) = [];
idx_pad([1 end],:) = [];
your_vals(2,2) = oldVal;
for ii = idx_pad(2:end);
[row col] = ind2sub(size(your_vals),ii);
%Finding neighbors
N = your_vals(row-1,col);
S = your_vals(row+1,col);
E = your_vals(row,col+1);
W = your_vals(row,col-1);
totBool = false;
%Checking neighbors
while ~totBool
your_vals(ii) = randi([4 18]);
comp = your_vals(ii);
boolN = isnan(N) || comp-N <= 4;
boolS = isnan(S) || comp-S <= 4;
boolE = isnan(W) || comp-W <= 4;
boolW = isnan(E) || comp-E <= 4;
totBool = boolN && boolS && boolE && boolW;
end
end
your_vals(:,[1 end]) = [];
your_vals([1 end],:) = [];

More Answers (2)

the cyclist
the cyclist on 5 Dec 2012
Edited: the cyclist on 5 Dec 2012
Generating random numbers that also obey certain restrictions is often a tricky business. I suggest that you edit your question to describe exactly the properties you want your random sequence to have.
From what you have written, it seems like you want your values to be within the range [4 18], but never have an absolute difference of more than 4 between consecutive. The following simple for loop will do that:
N = 100;
h = zeros(1,N);
h(1) = randi([4 18]);
for i = 2:N
h(i) = h(i-1) + randi([-4 4]);
h(i) = min(18,h(i));
h(i) = max(4, h(i));
end
However, if you make a histogram of h:
hist(h,unique(h))
you will see that the values are not uniformly distributed (which may be a property you were also hoping for). The end values have greater likelihood.
It may be that it is not possible to make a sequence with all the properties you want.

Rica
Rica on 5 Dec 2012
hi!
i used this matlab function to generate random numbers:
%
h=randi([4 18],100,5);
how could generate random number with a maximal diffrence of 1 to 4 between two successive numbers?
for exemple:
4 7 3 5 5 9 13 14 10.....
i hope you could help me! thanks ------------------------------------------------
My question is that you understood. But i want to generate it with 5 dimension (size(h)=100*5). thanks for the answer
  1 Comment
Jan
Jan on 6 Dec 2012
Edited: Jan on 6 Dec 2012
@Rica: I've moved the important information into the question. Of course a reader should find the exact and clear description of the problem there and not distributed over different answers (which are not answers) and comments. You can and should add clarifications by editing the question by your own in the future. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!