How generate random data that satisfy specific constraints such as having specific median? in R In general, how can I simulate data that exactly satisfies a set of constraints? I'm in need of generating a set of random numbers, which conform to a given median (not mean), and also fall within certain upper and lower endpoints. Using the statistics software that I have (R).
This question is motivated by a question on meta-analysis, wherein the concern is with being able to run statistical analyses on the data in question; in other words, once I generate the data set, I have to do ANOVA and other tests on it. (I'm fine with doing all that; I just can't figure out how to generate the data.)
 A: I'll assume you want an odd number of points, say $n=2k+1$. Simply generate one point at your desired median, another $k$ points between the minimum and the median, and another $k$ points between the median and the maximum.
minimum <- 0
med <- 1
maximum <- 3
k <- 5
c(runif(k,minimum,med),med,runif(k,med,maximum))
#  [1] 0.9372903 0.4796201 0.6430768 0.9087109 0.8994878 1.0000000 2.4847469
#  [8] 2.3950060 1.0392661 1.4928570 2.1116606

A: Do you have a desired shape of the simulated data?
One approach to generating data with certain exact values is to generate data from a candidate distribution (such as normal, beta, etc.) then transform the data to match.
A simple example to get the desired median is to generate your data from a normal distribution, calculate the median of the generated data and subtract that value from every data point (now the median will be 0), then add the desired median to every data point.
To get the the max and min correct as well will be a little more difficult, but you can get there by dividing the centered data points by a value before adding the desired median.
If the data truly needs to always be within a certain range, then starting with data from a beta distribution may work better.
Here is a quick R example:
> x <- rbeta(1000, 2, 2)
> x <- x - median(x)
> x <- x * abs(min( (5-1)/min(x), (10-5)/max(x) ))
> x <- x + 5
> median(x)
[1] 5
> range(x)
[1] 1.000000 9.338061

