How to generate random numbers with constraint?

10 views (last 30 days)
I need ideas how to generate random numbers in given range in example under...constraint is--->1st number has to be greater than 2nd, 2nd greater than 3rd...24th greater than 25th
for n = 1 : 25
a(n) = (1.2-0.05)*rand(1)+0.05;
end

Accepted Answer

Torsten
Torsten on 27 Apr 2022
a = (1.2-0.05)*rand(25,1)+0.05;
a = sort(a,'descend')
  6 Comments
MIch
MIch on 28 Apr 2022
upper bound is 1.2
lower bound is 0.05
no particular distribution
Torsten
Torsten on 28 Apr 2022
Edited: Torsten on 28 Apr 2022
b = (1.2-0.05)*rand(12,1)+0.05;
b = sort(b,'descent');
c = (1.2-b(3))*rand(5,1)+b(3);
c = sort(c,'descent');
d = (1.2-b(7))*rand(5,1)+b(7);
d = sort(d,'descent');
e = (1.2-b(9))*rand(3,1)+b(9);
e = sort(e,'descent');
a = [b,c,d,e]

Sign in to comment.

More Answers (3)

Steven Lord
Steven Lord on 27 Apr 2022
Edited: Steven Lord on 27 Apr 2022
Generate the numbers then call sort on the array.
By the way, you don't need to use a for loop here. The rand function can generate a vector of values with a single call.
a = (1.2-0.05)*rand(1, 25)+0.05
a = 1×25
0.2278 0.6887 0.1720 0.1546 0.7384 0.5730 0.9304 0.4400 0.7577 0.7744 1.1934 1.1811 0.6472 0.8133 1.0554 0.4156 0.3485 0.0974 0.4555 0.3982 0.3111 1.0316 0.4099 0.5257 0.4588
b = sort(a, 'descend')
b = 1×25
1.1934 1.1811 1.0554 1.0316 0.9304 0.8133 0.7744 0.7577 0.7384 0.6887 0.6472 0.5730 0.5257 0.4588 0.4555 0.4400 0.4156 0.4099 0.3982 0.3485 0.3111 0.2278 0.1720 0.1546 0.0974

Prakash S R
Prakash S R on 27 Apr 2022
If all you want is that the numbers are randomly drawn from the uniform distribution between 0.05 and 1.2, you could generate a as above, follwed by
a = sort(a, 'descend')
or simply
a = sort((1.2-0.05)*rand(1,25)+0.05, 'descend');

Walter Roberson
Walter Roberson on 28 Apr 2022
First branch consists of following numbers 1>2>3>4>5>6>7>8>9>10>11>12, second brach starts at number 3 of first branch and consist folloving numbers 3>13>14>15>16>17, third branch starts at number 7 of first branch 7>18>19>20>21>22 and fourth branch starts at number 9 of first branch 9>23>24>25.
format long g
rmin = 0.05;
rmax = 1.2;
UB = { [], %1 < nothing
[1] %2 < 1
[1:2] %3 < 1,2
[1:3] %4 < 1,2,3
[1:4] %5 < 1,2,3,4
[1:5] %6 < 1,2,3,4,5
[1:6] %7 < 1,2,3,4,5,6
[1:7] %8 < 1,2,3,4,5,6,7
[1:8] %9 < 1,2,3,4,5,6,7,8
[1:9] %10 < 1,2,3,4,5,6,7,8,9
[1:10] %11 < 1,2,3,4,5,6,7,8,9,10
[1:11] %12 < 1,2,3,4,5,6,7,8,9,10,11
[3] %13 < 3
[3 13] %14 < 3,13
[3 13:14] %15 < 3,13,14
[3 13:15] %16 < 3,13,14,15
[3 13:16] %17 < 3,13,14,15,16
[7] %18 < 7
[7 18] %19 < 7,18
[7 18:19] %20 < 7,18,19
[7 18:20] %21 < 7,18,19,20
[7 18:21] %22 < 7,18,19,20,21
[9] %23 < 9
[9 23] %24 < 9,23
[9 23:24] %25 < 9,23,24
};
NV = numel(UB);
V = zeros(NV,1);
V(1) = RR(rmin,rmax);
for K = 2 : NV
least = min(V(UB{K}));
V(K) = RR(rmin,least);
end
[[1:11].', V(1:11)]
ans = 11×2
1 0.509337598303387 2 0.139316553971349 3 0.0584882780365813 4 0.0505019413423992 5 0.0504884664739595 6 0.0500210905816074 7 0.0500035847219492 8 0.0500022318950057 9 0.0500016859195006 10 0.0500009862376525
[[3,13:17].', V([3,13:17])]
ans = 6×2
3 0.0584882780365813 13 0.0563547623292359 14 0.0557441291649364 15 0.0512888627397752 16 0.0512332793800842 17 0.05071245049519
[[7,18:22].', V([7,18:22])]
ans = 6×2
7 0.0500035847219492 18 0.0500021857353128 19 0.0500006725624357 20 0.0500000578760855 21 0.0500000018122051 22 0.0500000010119062
[[9,23:25].', V([9,23:25])]
ans = 4×2
9 0.0500016859195006 23 0.0500001392274942 24 0.0500001168263988 25 0.0500000781080973
function x = RR(rmin, rmax)
x = rand() * (rmax - rmin) + rmin;
end
  1 Comment
Walter Roberson
Walter Roberson on 28 Apr 2022
Notice how near the end, everything gets squashed into very close to the lower bound. This is to be expected for this kind of generating.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!