Library contents

Go rand.Float64

Draw a pseudorandom fraction for a simple simulation and check its expected range.

A simple example

rand.Float64

  1. Start with One random draw
  2. Do this Check its permitted range
  3. Result Inside the range
The value can change with each draw. Here the useful result is whether it is between zero and one.
main.go
package main

import (
    "fmt"
    "math/rand"
)

func main() {
    draw := rand.Float64()
    if draw >= 0 && draw < 1 {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}
Output
yes

Syntax

Go syntax
rand.Float64()
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Check 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.

Resultyes
main.go
package main

import (
    "fmt"
    "math/rand"
)

func main() {
    draw := rand.Float64()
    if draw >= 0 && draw < 1 {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}
Output
yes

The number itself varies. The range check produces the same answer on each run.

Advertisements

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.

GivenRecorded draw 0.625 · 8 stations
Worked result5

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.

GivenRecorded draw 0.000 · 6 tables
Worked result0

What to watch for

Use an appropriate secure generator for tokens and secrets.

Documentation for rand.Float64