The Random
class provides a random generator based on xoshiro256++.
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(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]