Created
September 30, 2021 13:52
-
-
Save leontabak/d5bc39915aa59d12263170c1c2c2beec to your computer and use it in GitHub Desktop.
Make list of simulated processes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.random.Random | |
// Create a list of processes. | |
// Each process has 2 properties: | |
// * it knows how much time has elapsed | |
// between the time that the previous | |
// process entered the waiting line | |
// and the time it took its place | |
// at the end of the line | |
// * it knows the time at which it | |
// joined the waiting line | |
// * this value must be computed | |
// * this computation requires a knowledge | |
// of the time at which the previous | |
// process joined the waiting line | |
// This program shows two ways of computing | |
// that second property. | |
data class Process( | |
val timeSinceArrivalOfLastProcess: Int, | |
val timeOfArrivalOfThisProcess: Int = 0 | |
) // Process | |
fun main(args: Array<String>) { | |
// create a random number generator | |
val rng = Random(System.nanoTime()) | |
// define a function that will return | |
// a random number drawn from a uniform | |
// distribution | |
val randomInteger = { rng.nextInt(1, 9) } | |
// define a function that, given an integer | |
// (the index of the element of a list), will | |
// return a Processor object whose first property | |
// is a random number and whose second property | |
// is the default value (0 in this case) | |
val makeProcess: (Int) -> Process = | |
{ _ -> Process(randomInteger()) } | |
// specify how big the list of customers | |
// should be | |
val size = 8 | |
// First way of solving problem... | |
// Create list of Processes | |
val q0 = List<Process>(size) { Process(randomInteger()) } | |
// Compute time of arrival of each process | |
// Do not change values of properties in processes. | |
// Instead, create new processes in a new queue. | |
val q1 = mutableListOf<Process>() | |
var prevTime = 0 | |
for( process in q0 ) { | |
val currTime = process.timeSinceArrivalOfLastProcess + | |
prevTime | |
val updatedProcess = | |
process.copy(timeOfArrivalOfThisProcess = currTime) | |
q1.add( updatedProcess ) | |
prevTime = currTime | |
} // for | |
// Second way of solving problem... | |
// Begin with an empty queue. | |
val queue = mutableListOf<Process>() | |
// timeSinceArrivalOfLastProcess and | |
// timeOfArrivalOfThisProcess are equal | |
// for the first process | |
var delay = randomInteger() | |
queue.add(Process(delay, delay)) | |
for (i in 1 until size) { | |
delay = randomInteger() | |
val time = queue[i - 1].timeOfArrivalOfThisProcess + | |
delay | |
queue.add(Process(delay, time)) | |
} // for | |
// Look at results | |
// Processes produced in the first way | |
for( process in q1 ) | |
println( process ) | |
println() | |
// Processes produced in the second way | |
for (process in queue) | |
println(process) | |
} // main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment