# Random Numbers

## Introduction

In the course of working with long numbers one may require a long stream of random numbers. Here we review methods for generating them.

## Irrational numbers

The digits of the irrational numbers such as **π** and **е** are randomly distributed but of course cannot be said to be truly random. they are psuedorandom in that they satisfy all of the properties of randomness but are in fact comletey predictable if we know the generating function.

Psuedonoise generators employ a fixed-length shift register that will emit a very long sequence of random numbers before they repeat exactly. If we know the exact taps of the shift register then we can predict the number sequence precisely.

Random numbers are:

* statistically independent
* not necessarily uniformly distributed
* create no visible pattern in an x-y plot:

![](file:xy.png)

## Random.org

The website <https://www.random.org/> provides an API that any remote application can access securly to obtain true random numbers.

## Random Number Widget.

The site has a little widget for generting a true random number between 1 and a maximum you specifiy. It [generates the HTM](https://www.random.org/widgets/integers/) for you. Here is an example (change wwww to www to enable it - GitBook crashes soon after ):

The numbers generated by this widget come from RANDOM.ORG's true random number generator.

## Coinflipper

The [Coinflipper](https://www.random.org/coins/) site will generate a sequence of pictures of the coin tosses of your choice. This is all in fun.

## Random Number Internet API

[Reference](https://www.random.org/clients/http/)

This example will generate a series of 10 integers in the \[1,6] interval:

```markup
https://www.random.org/integers/?num=10&min=1\
&max=6&col=1&base=10&format=plain&rnd=new
3
2
2
2
3
5
2
1
4
2
```

## Command Line Methods

Unix and Linux provide two system devices for generating random number streams:

* [/dev/random](https://en.wikipedia.org/wiki/dev/random)
* /dev/urandom
* /dev/hwrng (hardware random number generator)
* [echo $RANDOM](http://www.cyberciti.biz/faq/bash-shell-script-generating-random-numbers/)

An example method to generate 10 million random bytes (this takes almost one second on an early 2008 Mac Pro running 2.8 Ghz Xeon processor) on the command line is:

```bash
dd  bs=10000000  count=1 if=/dev/random  of=xxxx
```

On Linux, /dev/urandom (where u stands for unlimited) will reuse the entropy pool rather than blocking, while /dev/random will block when the entopy pool has drained.

To generate a sequence of random numbers in the bash shell:

```bash
for i in {1..5}; do echo $RANDOM; done
    32340
    18591
    32100
    15165
    19743
```

## Random Numbers in Programming Languages

### R

[Reference](http://blog.revolutionanalytics.com/2009/02/how-to-choose-a-random-number-in-r.html)

```r
x2 <- runif(10, 5.0, 7.5) # 10 numbers between 5.0 and 7.5
x3 <- sample(1:10, 1)     # 10 integers between 1 and 10
x5 <- sample(1:40, 6, replace=F) # 6  random numbers 1:40 w/o replacement
```
