Groovy Math.random
Generate a pseudorandom double in the interval from zero inclusive to one exclusive.
A simple example
Math.random
- Start with One random draw
- Do this Check its permitted range
- Result Inside the range
double draw = Math.random()
println(draw >= 0.0 && draw < 1.0)
true
Syntax
Math.random()
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovyCheck the range of a random draw
A teacher is testing the draw used to assign practice exercises. The actual draw changes on each run, so the test checks whether it stays inside the promised interval, including its lower and upper boundaries.
double draw = Math.random()
println(draw >= 0.0 && draw < 1.0)
true
Each draw can differ. Testing that it lies in the interval gives a reproducible true result.
Assign a rehearsal station
A practice exercise replays a recorded draw so the teacher can check the assignment. Use a draw from zero inclusive to one exclusive. Multiply by the number of available stations and take the integer part to select a zero-based station.
Assign the first activity table
A test fixture supplies the lower endpoint of the draw range. Use a draw from zero inclusive to one exclusive. Multiply by the number of available stations and take the integer part to select a zero-based station.
What to watch for
Use a cryptographic random generator for secrets. Math.random is intended for ordinary simulations.