Skip to content

Instantly share code, notes, and snippets.

@duncan
Last active August 29, 2015 14:02
Show Gist options
  • Save duncan/e897d70e0772c42623b5 to your computer and use it in GitHub Desktop.
Save duncan/e897d70e0772c42623b5 to your computer and use it in GitHub Desktop.
Fun with Scala to run something only on a smallish percentage of times
import scala.util.Random
// extracted from a conversation between duncan, chad, and ryan. Don’t take seriously, OK? :)
if (scala.util.Random.nextBoolean && scala.util.Random.nextBoolean) {}
// OR
if((1 to 2).map { _ => scala.util.Random.nextBoolean }.foldLeft(true){(f, v) => f && v }) {}
// OR
if ((1 to 2).map { _ => scala.util.Random.nextBoolean }.filter(!_).isEmpty) {}
// OR
if (Random.shuffle((1 to 2).map { _ => Random.nextBoolean }).take(1)) {}
// OR FINALLY
if (Random.nextInt(2) > 0) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment