I want to generate random number in sorted order. I wrote below code:
void CreateSortedNode(pNode head)
{
int size = 10, last = 0;
pNode temp;
while(
Suppose you wanted to generate just three random numbers, x
, y
, and z
so that they are in sorted order x <= y <= z
. You will place these in some C++ container, which I'll just denote as a list like D = [x, y, z]
, so we can also say that x
is component 0 of D
, or D_0
and so on.
For any sequential algorithm that first draws a random value for x
, let's say it comes up with 2.5, then this tells us some information about what y
has to be, Namely, y >= 2.5
.
So, conditional on the value of x
, your desired random number algorithm has to satisfy the property that p(y >= x | x) = 1
. If the distribution you are drawing from is anything like a common distribution, like uniform or Guassian, then it's clear to see that usually p(y >= x)
would be some other expression involving the density for that distribution. (In fact, only a pathological distribution like a Dirac Delta at "infinity" could be independent, and would be nonsense for your application.)
So what we can speculate with great confidence is that p(y >= t | x)
for various values of t
is not equal to p(y >= t)
. That's the definition for dependent random variables. So now you know that the random variable y
(second in your eventual list) is not statistically independent of x
.
Another way to state it is that in your output data D
, the components of D
are not statistically independent observations. And in fact they must be positively correlated since if we learn that x
is bigger than we thought, we also automatically learn that y
is bigger than or equal to what we thought.
In this sense, a sequential algorithm that provides this kind of output is an example of a Markov Chain. The probability distribution of a given number in the sequence is conditionally dependent on the previous number.
If you really want a Markov Chain like that (I suspect that you don't), then you could instead draw a first number at random (for x
) and then draw positive deltas, which you will add to each successive number, like this:
x
, say 2.5y-x
, say 13.7, so y
is 2.5 + 13.7 = 16.2z-y
, say 0.001, so z
is 16.201You just have to acknowledge that the components of your result are not statistically independent, and so you cannot use them in an application that relies on statistical independence assumptions.