I have 1000 uniformly distributed random numbers. How do I manipulate them to get a V-shaped histogram?

  • 2
    $\begingroup$ "V-shaped" is not a precise term. There is no way for us to know whether you mean something that looks like $f(x)=|x|$ on $x \in [-1,1]$, or $f(x) = 1-|x|$ on $x \in [-1,1]$, for instance. $\endgroup$
    – heropup
    Mar 23, 2014 at 18:02
  • $\begingroup$ I mean something like the $f(x)=|x|$ $\endgroup$
    – user137425
    Mar 23, 2014 at 18:12
  • $\begingroup$ $f(x) = |x|$ won't work if your data are $\mathcal U(0,1)$. Are your data uniform on (-1, 1)? Have you tried simply subtracting .5, multiplying by 2 & then taking the absolute value? Do you need the actual data to have the 'V-shaped' distribution, or just the histogram? $\endgroup$ Mar 23, 2014 at 18:31
  • $\begingroup$ I just want the histogram from uniformly distributed numbers. I don't have an actual data. $\endgroup$
    – user137425
    Mar 23, 2014 at 18:41
  • 1
    $\begingroup$ Is this for some subject? $\endgroup$
    – Glen_b
    Mar 23, 2014 at 21:44

5 Answers 5


Search for the "transformation method" or "inverse transform method", which is a way to generate random numbers with an arbitrary distribution. You'll find many lecture notes describing the idea. This is the wikipedia page: Inverse transform sampling. It has links to more detailed resources at the bottom.

The basic result is this recipe: If you need some distribution $D$, then

  1. find its CDF and invert it.
  2. generate uniformly distributed random numbers between 0 and 1
  3. transform these numbers with the inverse CDF to get numbers distributed according to $D$

The calculation can't be done analytically for every distribution. For your distribution it can. If the domain of the "V" is $[-1,1]$, then the PDF is $|x|$, the CDF is $(1+\operatorname{sign}(x) x^2)/2$, and the inverse CDF will be $$\operatorname{sign}(2x-1) \sqrt{|1-2x|}$$

For example, in Mathematica


If $X$ and $Y$ are independent uniformly distributed random variables on $[0,1]$, then $X + Y$ has a pyramid or "inverted V" shaped distribution on $[0,2]$.

All we need to do to turn this pyramid into a V is to swap the two halves of the distribution.

Thus, given independent $X, Y \sim \mathcal U(0,1)$, let $$Z = \begin{cases} X+Y & \text{if } X+Y < 1 \\ X+Y-2 & \text{otherwise.} \end{cases}$$

The random variable $Z$ will then have the V-shaped the distribution you want on $[-1,1]$.

  • $\begingroup$ Where does your $Y$ come from? $\endgroup$ Mar 23, 2014 at 23:40
  • 1
    $\begingroup$ @NickStauner: From the random number generator, same as $X$. I'm not sure I understand your question. $\endgroup$ Mar 23, 2014 at 23:47

This should really be a comment below the above answer. But since I do not have enough reputation to make this comment I will post it here. In the question you originally asked how to do this in "excel". This should do it,


An interesting note to the previous answer is that it does not change the distribution of the created variable at all if you are using one uniform random variable (the same for both) to generate the V-shaped distribution or two (for the sign function and the abs function).


You do want to use $U\sim \text{Uniform}(0,1)$. You are looking for something called Probability Integral Transform (PIT).

If $X \sim F$ then, $F(X)\sim \text{Uniform}(0,1)$. Therefore, first you will find the CDF for the distribution you are interested in, then you will transform. For example if you want $f(x)=|x|$, $x\in (-1,1)$, integrate to get $F(x)=P(X\leq x)=\begin{cases} \frac{-x^2}{2}+\frac{1}{2} & x\leq 0 \\ \frac{x^2}{2}+\frac{1}{2} & x> 0 \end{cases}$. Then, $F^{-1}(u) \sim F$


There are a variety of approaches that might be suitable.

Even with the comments you haven't really pinned it down enough (you give an example of what you want, but not what range of cases you want considered), but here are some examples of approaches:

1) rejection sampling. Generate a uniform on the desired range, and use rejection to obtain the desired distribution. This works quite generally, and can be made reasonably efficient even for fairly general cases. Variants of rejection sampling, such as the ziggurat approach, can be very fast, but may be quite fiddly to set up if you only want a few numbers.

2) For the case of $f(x) =|x|$ on $(-1,1)$. Let $U_1,U_2$ be iid standard uniform. Then $\text{max}(U_1,U_2)$ has a distribution like the positive half of the desired density. Let $Z$ be a random sign - i.e. $\{-1, +1\}$ with equal probability. Then $X=Z\,\text{max}(U_1,U_2)$ has the desired distribution.

3) (following the same set-up as in (2)): Let $V=\sqrt{U_1}$, and attach a random sign to that, $X=Z\,V$. (This uses the probability integral transform to get V of the right form).

There are innumerable other approaches with varying mixes of convenience and speed. For example, the approach in (2) can be used to generate two such variables at a time, and if speed is paramount, the bit required for a random sign may be taken from one of the uniforms used (preferably before normalizing to (0,1), and then bitshifting or a smaller scaling factor used to take what's left to still be uniform); this later approach might be used in (3) for example, or in a modified version of (1).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.