Tagged: Random number generation

Pseudo Random Number

Every random experiment results in two or more outcomes.
A variable whose values depend upon the outcomes of a random experiment is called a random variable denoted by capital letters X, Y, or Z and their values by the corresponding small letters x, y or z.

Random Numbers and their Generation

Random numbers are a sequence of digits from the set {0,1,2,⋯,9} so that, at each position in the sequence, each digit has the same probability 0.1 of being selected irrespective of the actual sequence, so far constructed.

The simplest ways of achieving such numbers are games of chance such as dice, coins, cards or by repeatedly drawing numbered slips out of a jar. These are usually grouped purely for the convenience of reading but this would become very tedious for long runs of each digit. Fortunately, tables of random digits are widely available now.

Pseudo Random Process

Pseudo-Random Process is a process that appears to be random but actually, it is not. Pseudo-random sequences typically exhibit statistical randomness while being generated by an entirely deterministic causal process. Such a process is easier to produce than a genuinely random one and has the benefit that it can be used again and again to produce exactly the same numbers and they are useful for testing and fixing software.

For implementation on computers to provide a sequence of such digits easily, and quickly, the most common methods are called Pseudo Random Technique.

Here, the digit will re-appear in the same order (cycle) eventually. For a good technique, the cycle might be tens of thousands of digit long.
Of course, the pseudo-random digits are not truly random. In fact, they are completely deterministic but they do exhibit most of the properties of random digits. Generally, their methods involve the recursive formula e.g.

\[X_{n+1}= a x_n +b\, mod\, m; n=0, 1, 2, …\]

a, b and n are suitably chosen integer constants and the seed $x_0$ (a starting number i.e. n = 0) is an integer. (Note mode $m$ means that if the result from the formula is greater than m, then divide it by m and keep the remainder as a random number.

Use of this formula gives rise to a sequence of integers each of which is in the random 0 to m – 1.

Example

let a = 13, b=5, and m = 1000, Generate 500 random numbers.

Solution

\[x_{n+1}=a x_n \,b\, mod\, 1000; n=0,1,2,…\]

let seed $x_0=5$, then for n=0 we have

\begin{align*}
x_{0+1}&=13 \times 5 +5\, mod\, 1000=70\\
x_{1+1}&=13 \times 70 + 5\, mod\, 1000=915
\end{align*}

Application of Random Variables

The random numbers have wide applicability in the simulation techniques (also called Monte Carlo Methods) which have been applied to many problems in the various sciences and one useful in the situation where direct experimentation is not possible, the cost of conducting an experimetn is very high or the experiment takes too much time.

R code to Generate Random Number

# store the pseudo random output
rand.num<-numeric(500)
rand.seed<-5
for(i in 1:500){
    rand.seed<-13*rand.seed+5
    rand.num[i]<-rand.seed%%1000
}
rand.num

Read more about Pseudo Random Process | Random Number Generation and Linear Congruential Generator (LCG)

Read more on Wikipedia: Pseudo Randon Numbers generator

Binomial Random number Generation in R

We will learn here how to generate Bernoulli or Binomial distribution in R with the example of a flip of a coin. This tutorial is based on how to generate random numbers according to different statistical distributions in R. Our focus is on binomial random number generation in R.

We know that in Bernoulli distribution, either something will happen or not such as coin flip has to outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For an unbiased coin, there will be a 50% chance that the head or tail will occur in the long run. To generate a random number that is binomial in R, use rbinom(n, size, prob) command.

rbinom(n, size, prob) #command has three parameters, namey

where
‘n’ is the number of observations
‘size’ is the number of trials (it may be zero or more)
‘prob’ is the probability of success on each trial for example 1/2

Some Examples

  • One coin is tossed 10 times with probability of success=0.5
    coin will be fair (unbiased coin as p=1/2)
    rbinom(n=10, size=1, prob=1/2)
    OUTPUT: 1 1 0 0 1 1 1 1 0 1
  • Two coins are tossed 10 times with probability of success=0.5
  • rbinom(n=10, size=2, prob=1/2)
    OUTPUT: 2 1 2 1 2 0 1 0 0 1
  • One coin is tossed one hundred thousand times with probability of success=0.5
    rbinom(n=100,000, size=1, prob=1/2)
  • store simulation results in $x$ vector
    x<- rbinom(n=100,000, size=5, prob=1/2)
    count 1’s in x vector
    sum(x)
    find the frequency distribution
    table(x)
    creates a frequency distribution table with frequency
    t = (table(x)/n *100)}
    plot frequency distribution table
    plot(table(x),ylab = "Probability",main = "size=5,prob=0.5")

View Video tutorial on rbinom command

x Logo: Shield Security
This Site Is Protected By
Shield Security