The Random class provides a random generator based on xoshiro256++.

Static Methods

new(seed)

Create a new random object initialized with seed.

def r = Random.new(42)

new(seed0, seed1, seed2, seed3)

Create a new random object initialized with seed0, seed1, seed2 and seed3. This initializes the full state (256 bits) of the random generator.

def r = Random.new(42, 69, 666, -1)

Methods

clone()

Get a copy of the random object. The sequence given by the clone and the original object will be the same.

def r0 = Random.new(42)
def r1 = r0.clone()
IO.println(r0.int() == r1.int()) #> true

float()

Return a Float between 0.0 (included) and 1.0 (excluded) uniformly.

def r = Random.new(42)
IO.println(r.float()) #> 0.81430514512291

float(end)

Return a Float between 0.0 (included) and end (excluded) uniformly.

def r = Random.new(42)
IO.println(r.float(Math.PI)) #> 2.5582150616985

float(start, end)

Return a Float between start (included) and end (excluded) uniformly.

def r = Random.new(42)
IO.println(r.float(Math.PI2, Math.PI)) #> 2.8499038576441

int()

Return an Int uniformly.

def r = Random.new(42)
IO.println(r.int()) #> -3425465463722317665

int(end)

Return an Int between 0 (included) and end (excluded) uniformly.

def r = Random.new(42)
IO.println(r.int(69)) #> 55

int(start, end)

Return an Int between start (included) and end (excluded) uniformly.

def r = Random.new(42)
IO.println(r.int(-69, 69)) #> -14

sample(array)

Return a random element from array.

def r = Random.new(42)
IO.println(r.sample([ 1, 'a', 3.14, "good" ])) #> good

shuffle(array)

Shuffle the elements in the array using Fisher–Yates shuffle and return the array.

def r = Random.new(42)
IO.println(r.shuffle([ 1, 'a', 3.14, "good" ])) #> [good, 1, 3.14, a]