Skip to content

Instantly share code, notes, and snippets.

@Ehsan1997
Last active March 17, 2017 18:53
Show Gist options
  • Save Ehsan1997/314b5a9da5077e4101b944836d111673 to your computer and use it in GitHub Desktop.
Save Ehsan1997/314b5a9da5077e4101b944836d111673 to your computer and use it in GitHub Desktop.
A program to estimate number of users that a network could support with packet switching by entering the required parameters.
//Author Name : Muhammad Ehsan ul Haq
fun main(args : Array<String>) {
//active probability totalRate/UserNeededRate Required probability
var Users = UsersNumberPrediction(0.25, 8000/500, 0.9)
println("Number of Users that can be connected simultaneously using Packet Switching = " + Users)
println("SMG = " + Users.toDouble()/(8000.0/500.0))
}
fun factorial(A: Int) : Double{
if(A <= 1)
return 1.0
return A * factorial(A - 1)
}
fun UsersNumberPrediction(p : Double, X : Int, expProb : Double) : Int{
var i = 1
while(CumulativeBinomialDistribution(p, i, X) >= expProb ){
i++
}
return --i
}
fun CumulativeBinomialDistribution(p: Double, N : Int, X :Int) : Double{
if(X == 0)
return BinomialDistribution(p, N, 0)
return BinomialDistribution(p, N, X) + CumulativeBinomialDistribution(p, N, X - 1)
}
fun BinomialDistribution(p: Double, N : Int, X : Int) : Double{
val NFac = factorial(N)
val XFac = factorial(X)
val DiffNX = factorial(N - X)
val PPowerX = Math.pow(p, X.toDouble())
val OneMinusProb = Math.pow((1-p), (N - X).toDouble())
return (NFac / (XFac * DiffNX)) * PPowerX * OneMinusProb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment