jeudi 30 novembre 2017

Scala - Call a random method - not twice

I have several methods which falsify a word, and I need to call them randomly, until one achieve to create a "human mistake".

I want to call a random method then another, until it's ok, but never twice the same.
Putting them in an array (or a list) imply that I rewrite an array each time I try a method, it's an ugly computational complexity and I'd like to write "Scala style" code, with minimum of var an mutability.

This is a simplified version of the code:

val rand: Random = new Random()

def m1(): Boolean = rand.nextBoolean()
def m2(): Boolean = rand.nextBoolean()
def m3(): Boolean = rand.nextBoolean()
def m4(): Boolean = rand.nextBoolean()

def tryUntilOk(): Unit = {
  // Try a random method
  // If the result isn't satisfying, try another
  // Etc until result is ok OR no method left
}

If you think it's not possible without rewriting a collection each time tell me
A java solution could help anyway




Aucun commentaire:

Enregistrer un commentaire