R runif
Draw a pseudorandom fraction for a simple simulation and check its expected range.
A simple example
runif
- Start with One random draw
- Do this Check its permitted range
- Result Inside the range
draw <- runif(1)
cat(if (draw >= 0 && draw < 1) "yes" else "no", "\n", sep = "")
yes
Syntax
runif(1)
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.RCheck the range of a practice 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.
draw <- runif(1)
cat(if (draw >= 0 && draw < 1) "yes" else "no", "\n", sep = "")
yes
The number itself varies. The range check produces the same answer on each run.
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 an appropriate secure generator for tokens and secrets.