generate random numbers between limits and satisfy condition

Hello All,
I am trying to generate two sets of 16 length random number sequences.
x1, x2, x3 ...x16
y1, y2, y3 ...y16
Conditions:
(x,y) lies between 5000 and 6000
The separation between x(n) and x(n+1) should be between +/-(20 - 100). It need not be equally spaced.
The separation between x(n) and y(n) should be +/- 300. The y(n) also should belong to 5000 - 6000 range
I need to check how many such sets can satisfy these conditions.
Kindly help me with a logic or the best way to implement in MATLAB

Answers (1)

How about something like this?
a = 5000 ; b = 6000 ;
x = zeros(1,16) ;
x(1) = randi([a b],1,1) ;
for i = 2:16
x(i) = randi([x(i-1)+20 x(i-1)+100],1,1) ;
end
diff(x)

3 Comments

a = 5000;
b = 6000;
x(1) = randi([a b],1,1);
for i = 2:16
x(i) = randi([x(i-1)-20 x(i-1)+100],1,1);
end
x
x =
Columns 1 through 10
5916 5991 6087 6146 6130 6212 6305 6367 6438 6507
Columns 11 through 16
6534 6593 6593 6658 6641 6654
5916-5991
ans =
-75
6087-5991
ans =
96
6654-6641
ans =
13
Thank you for your time and prompt response. It does generate numbers between the range, but the difference is not maintained between 20 and 100, As shown above, I got difference between two consequitive numbers as 13.
Also if the x and y separation needs to be maintained, only then I can consider x and y sequences. It has to meet all the conditions to be eligible.
Kindly advise. Thanks in Advance.
Edited the code.... generating y you have to try. :)
Thank you again for your respone. I generated Y sequence. but the diff is not maintained for Y. (Please see the attached screenshot)
Also, I was wondering by doing [x(i-1)+20 x(i-1)+100] aren't we missing out the other numbers as in,
if x(i-1) = 5510,
x(i-1)+20 = 5530, aren't we missing x(i-1)-20 = 5490. Similarly for x(i-1)+100.
Also, it is important that x(i-1)+20 / x(i-1) -20/ x(i-1)+100 / x(i-1)-100 should belong to the range of 5000 and 6000
By ignoring those numbers, Am i going to miss number of sequences that can be formed with these conditions. Kindly advise

Sign in to comment.

Categories

Asked:

on 12 Feb 2021

Edited:

on 12 Feb 2021

Community Treasure Hunt

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

Start Hunting!