Is there any easy way to generate random numbers with a certain median and 1st and 3rd quartiles? I'm an assistant professor at a university,
and I want to generate some fake data for a course assignment.
How do I generate random numbers with a certain median and 1st and 3rd quartiles?
For example, I want to generate 100 random numbers,
so that the median is approximately 78,
the 1st quartile is approximately 52,
and the 3rd quartile is approximately 97.
 A: The ideal way to do this (though not necessarily the easiest) would be to set these order statistics equal to the required values, and then generate random values in the four quartiles bounded by these values using the conditional distribution of the order statistics in these intervals.  The problem with doing it this way is that conditional distribution of order statistics is complicated, so even with a simple underlying sampling distribution, this will be quite a hard problem.
If you are not too worried about the shape of the underlying sampling distribution then you can just generate any old random values in the four intervals demarcated by your three values, and this will give you a mock dataset with the required quantiles.  If you would like to make it match roughly to a desired distributional shape without going to the trouble of deriving conditional distributions of order statistics, you could match your mock data set up to the desired theoretical distribution using a QQ plot, and adjust your generated data points manually until you get a reasonable fit.
Here is an example of the process:

*

*Set your sample size $n = 4k + 3$ where $k$ is some non-negative integer.


*Set the desired quantiles:
$$\begin{equation} \begin{aligned}
\text{Lower Quartile} &= X_{(k+1)} \text{ } = 52, \\[4pt]
\text{Median} &= X_{(2k+2)} = 78, \\[4pt]
\text{Upper Quartile} &= X_{(3k+3)} = 97. \\[4pt]
\end{aligned} \end{equation}$$

*

*Now generate $k$ random values in each of the four quartiles demarcated by these values.  Assuming you would like non-negative integer values you could generate using distributions on the following supports:$^\dagger$
$$\begin{equation} \begin{aligned}
X_{(1)}, ..., X_{(k)} &\sim \text{Dist}[0, 52], \\[4pt]
X_{(k+2)}, ..., X_{(2k+1)} &\sim \text{Dist}[52, 78], \\[4pt]
X_{(2k+3)}, ..., X_{(3k+2)} &\sim \text{Dist}[78, 97], \\[4pt]
X_{(3k+4)}, ..., X_{(4k+3)} &\sim \text{Dist}[97, 100], \\[4pt]
\end{aligned} \end{equation}$$

*

*As stated, ideally the distributions you use for generating these values would be conditional distributions of order statistics, bounded by the required quantile values.  This would be based on some underlying desired sampling distribution.  If that is too complicated, just generate values from any reasonable distribution on those supports and then compare your results to your desired distribution on a QQ plot.  Adjust values manually if you want a better fit to some distribution.


$^\dagger$ As whuber points out in the comments, it is possible to extend this method for an arbitrary distribution on the real numbers by changing the lower and upper limits to negative and positive infinity respectively.  Here I am using the bounds for the possible number of marks out of one-hundred.
A: Just generate any data. For example normal, from N(0,1).
Find the first quartile, median, and third quartile of this data.
Then distort the initial data, such that the first quartile gets mapped to the desired value, the median to the desired median, etc. - use a piecewise linear map, for example. Unfortunately the outermost two, use the same linear scaling as the neighbor.
So we're simply stretching and squeezing the values a little bit to get the desired quantiles.
So assuming I want the quartiles to be 0 10 100, and I generated the data 0 1 2 3 4 5 6 7 8 9 10. I know I want to map 2 to 0, 5 to 50, and 8 to 100. So 2 to 5 will be linearly mapped to 0 to 10, for example. The resulting data then is approximately -6.67 -3.33, 0, 3.33, 6.67, 10, 40, 70, 100, 130, 160.
For better results, use e.g. a polynomial.
