three touple Random number generation

How to generate three touple (a,b,c) random number such that a < b < c. Please help

Answers (2)

John D'Errico
John D'Errico on 27 Apr 2020
Edited: John D'Errico on 27 Apr 2020
You say nothing about the distribution of the desired numbers. But why not try something? For example, what does this do?
cumsum(rand(1,3))
Or this?
sort(rand(1,3))
Depending on the desired distribution, I can think of at least a few other ways to do so.

2 Comments

I'm running a distribution test at the moment, including one approach that involves generating a first number, generating more numbers until one is larger than the first, then generating more until one is larger still. But it generated 0.999994<something> as the first number, and it sure is taking a long time to generate values that are larger... Lesson: Don't DO that approach.
Yep. There are always some obscenely bad ways to solve any problem. ;-)

Sign in to comment.

A variety of methods, with their statistical properties plotted.
For practical purposes, I had to put limit counts on the method of randomly selecting numbers until you found one that was larger than the previous. When you run the code, you will see a few lines similar to
Iteration 3372 of trial and error approach, giving up trying to surpass n(2) = 0.9999801343051803
which indicates that on repetition #3372, it gave up on trying to find a 3rd value to surpass a second value that was over 0.99998. In such a case, the code goes back and generates a new value for the item it cannot surpass. (Now that I think of it, there is a flaw in the rejection logic for the case that the initial value was really big.)
N = 10000;
f = {@rsort, @rtrial, @rreject, @rcumsum, @rcrand};
fn = {'sort', 'trial and error', 'rejection', 'cumsum', 'conditional random'};
nf = length(f);
r_3 = zeros(N,3,nf);
c3 = zeros(1,nf);
for fi = 1 : nf
alg = fn{fi};
[r_3(:,:,fi), c3(fi)] = f{fi}(N, alg);
fprintf('%s approach required %d random numbers\n', alg, c3(fi));
for col = 1 : 3
ax = subplot(nf, 3, (fi-1)*3+col);
histogram(ax, r_3(:,col,fi), 0:.05:1);
title(sprintf('%s %d', alg, col));
end
drawnow;
end
function [r, rc] = rsort(N, ~)
r = zeros(N, 3);
rc = 0;
for K = 1 : N
r(K,:) = sort(rand(1,3));
rc = rc + 3;
end
end
function [r, rc] = rtrial(N, alg)
r = zeros(N, 3);
rc = 0;
for K = 1 : N
n = zeros(1,3);
w = 1;
while w < 4
gotw = false;
for c = 1 : 1e5
n(w) = rand();
rc = rc + 1;
if w == 1 || n(w) > n(w-1)
gotw = true;
break;
end
end
if ~gotw
fprintf('Iteration %d of %s approach, giving up trying to surpass n(%d) = %.16f\n', K, alg, w-1, n(w-1));
w = w - 1;
else
w = w + 1;
end
end
r(K,:) = n;
end
end
function [r, rc] = rreject(N, ~)
r = zeros(N, 3);
rc = 0;
for K = 1 : N
while true
n = rand(1,3);
rc = rc + 3;
if n(1)<=n(2) && n(2) <= n(3); break; end
end
r(K,:) = n;
end
end
function [r, rc] = rcumsum(N, ~)
r = zeros(N, 3);
rc = 0;
for K = 1 : N
r(K,:) = cumsum(rand(1,3))/3;
rc = rc + 3;
end
end
function [r, rc] = rcrand(N, ~)
%conditional random -- select at random out of space that is left
r = zeros(N, 3);
rc = 0;
for K = 1 : N
n(1) = rand(1);
n(2) = n(1) + rand(1) * (1-n(1));
n(3) = n(2) + rand(1) * (1-n(2));
r(K,:) = n;
rc = rc + 3;
end
end

2 Comments

Notice that the different methods result in different statistical properties.
The sort method is easy and fast. It only needs one random number generated for each output number generated, so it is efficient. But we could hypothesize that it might somehow give a different distribution than the case where we generate 3 random numbers and keep the list if they happen to be sorted. We should therefore compare the statistics of the rsort and rreject algorithms... and we see that they are the same. Both of them involve significant biases towards "small" numbers for the first output, and towards "large" numbers for the third output, and to values around 1/2 for the second output.
The trial-and-error algorithm involves generating one number to start, and then keep generating numbers until one that is larger is generated, and then keep generating numbers until a larger one still is generated. This generates very different statistical properties for the first number, which can be nearly anywhere in range. However, this method very much tends to crowd the second and third number to the larger end of the range. It also involves so much rejection of numbers that for practical purposes I had to put in limits and force it to backtrack if it was taking too long to generate the numbers. For example, if the initial number was about 0.99999 then to randomly select two numbers larger could take a long time. And in theory since the random number generator generates from a discrete field by default, then it might generate the largest possible value, so it might be impossible to generate a larger value using that method... so limits are needed.
But if you compare the statistical output of the trial-and-error algorithm to the properties of the conditional probability algorithm, rcrand, then you will see they are the same -- and the conditional probability does not involve any rejection at all. The conditional probability algorithm involves generating a number within the space that is remaining in the interval.
The statistical probabilities of the cumsum algorithm are quite different than the ones mentioned above. In the above algorithms, the maximum value that can be generated is 1.0. If you were to just cumsum(rand(1,3)) then you would have a maximum value of 1+1+1 = 3, so you need to divide the values by 3 to scale them appropriately. But when you do, you will see that the largest value clusters around 0.5, with most of the values for the first two numbers being in the first half of the range. This does make sense mathematically: the mean value for each generated number is 0.5, add three of those you get 1.5, divide by 3 and you are back to a mean of 0.5 for the largest value. Mathematically, you approximate a normal distribution with mean 0.5 for the third value.

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 27 Apr 2020

Commented:

on 29 Apr 2020

Community Treasure Hunt

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

Start Hunting!