# [R] random numbers

Petr Savicky savicky at cs.cas.cz
Mon Feb 20 08:20:02 CET 2012

```On Sun, Feb 19, 2012 at 08:39:13PM -0800, Ryan Murphy wrote:
> Hello,
>
> Is there a way to create random numbers that fit a certain specified
> requirement other than distributional characteristics.
> In particular, I would like to create simulated income distributions with
> certain gini coefficient

Hello:

The gini coefficient does not determine the distribution uniquely,
so we have to make a choice of the distribution. According to the
article

http://en.wikipedia.org/wiki/Gini_coefficient

we have

(A) The gini coefficient for the log-normal distribution is

2 \Phi(sigma/sqrt(2)) - 1

(B) The gini coefficient for a finite sample y_i, i=1, ..., n is in R's syntax

2*sum(seq(length=n)*sort(y))/(n*sum(y)) - (n+1)/n

This suggests the following.

1. Generate a sample y with gini coefficient G using (A).

G <- 0.3
n <- 1000
sigma <- sqrt(2) * uniroot(function(x) { pnorm(x) - (G + 1)/2 }, c(0, 1e10))\$root
y <- exp(rnorm(n, sd=sigma))

2. Check the gini coefficient of the sample using (B).

2*sum(seq(length=n)*sort(y))/(n*sum(y)) - (n+1)/n

[1] 0.305003

Hope this helps.

Petr Savicky.

```