# R Programming/Random Number Generation

R Programming
* R Basics
** Introduction
** Sample Session
** Manage your workspace
** Settings and Installation
** Documentation
** Control Structures
** Working with functions
** Debugging
** Using C or Fortran
** Utilities
** Estimation utilities
** Packages
* Data Management
** Data types
** Working with data frames
** Importing and exporting data
** Text Processing
** Times and Dates
* Graphics
** Grammar of graphics
* Publication quality output
* Descriptive Statistics
* Mathematics
** Optimization
** Probability Distributions
** Random Number Generation
* Statistical Core Methods
** Maximum Likelihood
** Method of Moments
** Bayesian Methods
** Bootstrap
** Multiple Imputation
** Nonparametric Methods
* Regression Models
** Linear Models
** Quantile Regression
** Binomial Models
** Multinomial Models
** Tobit And Selection Models
** Count Data Models
** Duration Analysis
* Time Series
* Factor Analysis
* Classification
** Ordination
** Clustering
* Network Analysis
* High Performance Computing
** Profiling R code
** Parallel computing with R
* Appendix
** Sources
** Index

## Random Number Generators

To a very high degree computers are deterministic and therefore are not a reliable source of significant amounts of random values. In general pseudo random number generators are used. The default algorithm in R is Mersenne-Twister but a long list of methods is available. See the help of RNGkind to learn about random number generators.
It is possible to use true random numbers. Some of them are collected on random.org (link). The random (link) package gives an access to them.

### Randu

Randu is an old linear congruential pseudorandom number generator. There is a dataset generated with Randu in the datasets package. The function which is used to generate the dataset is in the help of this page.

## Seed

A pseudo random number generator is an algorithm based on a starting point called "seed". If you want to perform an exact replication of your program, you have to specify the seed using the function set.seed. The argument of set.seed has to be an integer.

## Sampling in a vector

Toss 10 coins
Roll 10 dice
play lottery (6 random numbers out of 49 without replacement)
You can sample in a multinomial distribution :

## Sampling in a standard univariate distribution

You can use rnorm, rt, etc.

### Misspecified argument

Note that if you put as argument of rnorm a vector instead of a number, R takes by default the length of the vector instead of returning an error. Here is an example :

## Inverse CDF method

* If you know the inverse CDF (quantile function), you can generate the random variable by sampling in the standard uniform distribution and transforming using the CDF.
For instance, if you want to simulate from a standard normal distribution, you can simulate from a standard uniform and transform it using the quantile function of the normal distribution.
This gives the same results as the rnorm function but the computing time is higher :

## Importance sampling

* See Jeff Gill's routine

## Gibbs algorithm

* rgs package (link)

## Quasi random numbers

* quasi RNG
Sometimes you need to generate quasi random sequences. The randtoolbox library provides several quasi random number generators.
See also sHalton and QUnif (sfsmisc).
* Halton Sequence
You can compare Halton draws with the standard R (pseudo) random number generator. Halton draws are much more systematic.

### Examples
## Resources

* Revolution Computing entry on pseudo random numbers
* Statistical Distributions Module Wessa.net it is online application which generates random numbers using R. You can have access to the R code and use it in your own programs.