By Alvin Alexander. Last updated: February 21, 2021
As a short “note to self,” I just used this Scala code to (a) create a list that contains random strings of different lengths, then (b) shuffle the list of strings to create a more random effect:
def randomString(length: Int) = scala.util.Random.alphanumeric.take(length).mkString def fakeDataForListView(): Array[String] = { val strings = List( randomString(10), randomString(12), randomString(14), randomString(16), randomString(18) ) scala.util.Random.shuffle(strings).toArray }
Sample output looks like this in the Scala REPL:
scala> fakeDataForListView() res0: Array[String] = Array(echWFdISlPJeb7oc48, Eb33McbK2DtlxOXJ, jvhIPiizluur, 3d7sNusuu0, H9oITRk36noQkh)
I convert the List
to an Array
at the very end because I’m using this code with the JavaFX ListView
, which only knows about arrays.