LoginSignup
1
1

More than 5 years have passed since last update.

Groovyで整数範囲(IntRange)を拡張してその範囲でランダムな値を返せるようにする

Last updated at Posted at 2012-12-02
intRangeRandom.groovy
IntRange.metaClass.define {
  random {
    [delegate.from, delegate.to].min() + new Random().nextInt(delegate.size())
  }
}

assert (1..10).random() in 1..10 // => 1〜10
assert (5..<1).random() in 2..5  // => 2〜5
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1