Skip to content

lukewilliamboswell/roc-random

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Randomness for Roc

A Roc library for random number generation

Status

Forked from JanCVanB/roc-random, updated to build package and documentation, and do some maintenance -- we will move it back to JanCVanB soon I'm just helping with all the breaking changes.

This packages has basic functionality, but there's much more it could do. Any contributions & feedback are very welcome!

Examples

# Print a list of 10 random numbers in the range 25-75 inclusive.
main! = |_args|
    random_numbers
    |> List.map(Num.to_str)
    |> Str.join_with("\n")
    |> Stdout.line!

numbers_generator : Random.Generator (List U32)
numbers_generator =
    Random.list(Random.bounded_u32(25, 75), 10)

random_numbers : List U32
random_numbers =
    # we can ignore the updated seed value and just return the generated numbers
    { value: numbers } = Random.step(Random.seed(1234), numbers_generator)

    numbers

expect
    actual = random_numbers
    actual == [52, 34, 26, 69, 34, 35, 51, 74, 70, 39]

See the examples/*.roc files for more examples.

Documentation

See the library documentation site for more info about its API.

Goals

  • An external API that is similar to that of Elm's Random library
  • An internal implementation that is similar to that of Rust's Rand library
  • Compatible with every Roc platform (though some platforms may provide poor/constant seeding)
  • Provides a variety of ergonomic abstractions

Seeding

In order to receive a different sequence of outputs from this library between executions of your application, your Roc platform of choice must provide a random/pseudorandom/varying seed.

Otherwise, your pure functions will be responsible for providing Random's pure functions with a constant seed that will merely choose which predictable sequence you'll receive.

About

🦅 🎲 Roc library for random number generation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 60.5%
  • CSS 16.9%
  • Roc 12.3%
  • JavaScript 8.0%
  • Shell 1.2%
  • Nix 1.1%