You are on page 1of 10

COMMUNICATION SIMULATION LAB

Experiment -1
a)Write a program to generate the pseudo random sequence with (0,1)
b)Write a program to generate uniformly distributed random numbers
c)Gaussian d)Exponential e)Rayleigh f)Lognormal
Theory:
Pseudorandom - Having the appearance of randomness, but nevertheless exhibiting a
specific, repeatable pattern. Random numbers are very difficult to generate, especially on
computers which are designed to be deterministic devices. The sequence is not truly random in
that it is completely determined by a relatively small set of initial values, called the PRNG's
state. A set of values or elements that is statistically random, but it is derived from a known
starting point and is typically repeated over and over. Pseudo-random numbers provide
necessary values for processes that require randomness, such as creating test signals or for
synchronizing sending and receiving devices in a spread spectrum transmission. Two important
statistical properties of pseudo random numbers are independence and uniformity. These
numbers are used in cryptography to provide end-to-end encryption. We can expect the
following properties from pseudo random generator:
Unbiasedness: The output of the generator has good statistical characteristics.
Unpredictable: Given a few first bits, it should not be easy to predict, or compute, the rest of
the bits.
Flowchart:
a)Code and plots for PRS:
import numpy as np
from matplotlib import pyplot as plt

def pseudo_random(a=16807,mod=(2**31)-1,seed=123456789):
u=np.zeros(1000, dtype=int)
x=(seed*a+1)%mod
u[0]=x%2
for i in range(1, 1000):
x=(x*a+1)%mod
u[i]=x%2
return u

def pdf(psr):
plt.hist(psr, bins=20, edgecolor='k', density=True)
plt.xlim(-2, 2)
plt.xlabel('random variable')
plt.ylabel('Probability density ')
plt.title('PDF of PSR')
h=plt.gcf()
plt.show()
return h

def cdf(psr):
plt.hist(psr, bins=50, histtype='step', cumulative=True, label='cdf',
density=True)
plt.xlim(-1, 1)
plt.xlabel('random variable')
plt.ylabel('Probability ')
plt.title('CDF of PSR')
h = plt.gcf()
plt.show()
return h

def autocorr(psr):
plt.acorr(psr, maxlags=20)
ac=plt.gcf()
plt.title('autocorrelation')
plt.show()
return ac

psr=pseudo_random()
print(psr)
psr0=psr.astype(float)
PDF=pdf(psr)
CDF=cdf(psr)
acor=autocorr(psr0)
b)Code and plots for uniform:
import numpy as np
from matplotlib import pyplot as plt
def pseudo_random(a=16807,mod=(2**31)-1,seed=123456789):
u=np.zeros(10000)
x=(seed*a+1)%mod
u[0]=x/mod
for i in range(1, 10000):
x=(x*a+1)%mod
u[i]=(x/mod)
return u

def pdf(psr):
plt.hist(psr, bins=50, histtype='step', label='pdf', density=True)
plt.xlim(0, 1)
plt.xlabel('random variable')
plt.ylabel('Probability denisty')
plt.title('PDF of uniform')
h=plt.gcf()
plt.show()
return h

def cdf(psr):
cd=plt.hist(psr, bins=50, histtype='step', cumulative=True,
label='cdf', density=True)
plt.xlim(0, 1)
plt.xlabel('random variable')
plt.ylabel('Probability ')
plt.title('CDF of uniform')
h = plt.gcf()
plt.show()
return h

def autocorr(psr):
plt.acorr(psr, maxlags=20)
ac=plt.gcf()
plt.title('autocorrelation')
plt.show()
return ac

psr=pseudo_random()
print(psr)
PDF=pdf(psr)
CDF=cdf(psr)
acor=autocorr(psr)
d)Code and plots for exponential:
import numpy as np
from matplotlib import pyplot as plt
def pseudo_random(a=16807,mod=(2**31)-1,seed=123456789):
u=np.zeros(1000)
x=(seed*a+1)%mod
u[0]=x/mod
for i in range(1, 1000):
x=(x*a+1)%mod
u[i]=(x/mod)
return u

def pdf(exp):
plt.hist(exp, bins=100, histtype='step', cumulative=-1, density=True)
plt.xlim(0, 10)
plt.xlabel('random variable')
plt.ylabel('Probability denisty')
plt.title('PDF of exponential')
h = plt.gcf()
plt.show()
return h

def cdf(psr):
plt.hist(psr, bins=50, histtype='step', cumulative=True, label='cdf',
density=True)

plt.xlim(0, 6)
plt.xlabel('random variable')
plt.ylabel('Probability ')
plt.title('CDF of exponential')
h = plt.gcf()
plt.show()
return h

def autocorr(exp):
plt.acorr(exp, maxlags=20)
ac = plt.gcf()
plt.title('autocorrelation')
plt.show()
return ac

psr=pseudo_random()
exp=(-1/10)*(np.log(1-psr))*10
PDF=pdf(exp)
CDF=cdf(exp)
acor=autocorr(exp)
e) Code and plots for Rayleigh:
import numpy as np
from matplotlib import pyplot as plt
def pseudo_random(a=16807,mod=(2**31)-1,seed=123456789):
u=np.zeros(1000)
x=(seed*a+1)%mod
u[0]=x/mod
for i in range(1, 1000):
x=(x*a+1)%mod
u[i]=(x/mod)
return u

def pdf(exp):
plt.hist(exp, bins=50, histtype='step', cumulative=-1, density=True)
plt.xlim(0, 10)
plt.xlabel('random variable')
plt.ylabel('Probability denisty')
plt.title('PDF of rayleigh')
h = plt.gcf()
plt.show()
return h

def cdf(psr):
plt.hist(psr, bins=50, histtype='step', cumulative=True, label='cdf',
density=True)
plt.xlim(0, 3)
plt.xlabel('random variable')
plt.ylabel('Probability ')
plt.title('CDF of rayleigh')
h = plt.gcf()
plt.show()
return h

def autocorr(exp):
plt.acorr(exp, maxlags=20)
ac = plt.gcf()
plt.title('autocorrelation')
plt.show()
return ac

psr=pseudo_random()
sigma=1
rayleigh=sigma*(np.sqrt(-1*np.log(psr)))
PDF=pdf(rayleigh)
CDF=cdf(rayleigh)
acor=autocorr(rayleigh)

You might also like