Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elm/random
Generate random values in Elm
https://github.com/elm/random
elm generator random rng
Last synced: about 1 month ago
JSON representation
Generate random values in Elm
- Host: GitHub
- URL: https://github.com/elm/random
- Owner: elm
- License: bsd-3-clause
- Created: 2018-03-09T03:26:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-21T09:43:33.000Z (9 months ago)
- Last Synced: 2024-09-30T10:03:30.634Z (about 1 month ago)
- Topics: elm, generator, random, rng
- Language: Elm
- Homepage: https://package.elm-lang.org/packages/elm/random/latest/
- Size: 45.9 KB
- Stars: 45
- Watchers: 8
- Forks: 23
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ccamel - elm/random - Generate random values in Elm (Elm)
README
# Randomness
Need to generate random numbers? How about random game boards? Or random positions in 3D space? This is the package for you!
## Example
This package will help you build a random `Generator` like this:
```elm
import Randomprobability : Random.Generator Float
probability =
Random.float 0 1roll : Random.Generator Int
roll =
Random.int 1 6usuallyTrue : Random.Generator Bool
usuallyTrue =
Random.weighted (80, True) [ (20, False) ]
```In each of these defines _how_ to generate random values. The most interesting case is `usuallyTrue` which generates `True` 80% of the time and `False` 20% of the time!
Now look at this [working example](https://guide.elm-lang.org/effects/random.html) to see a `Generator` used in an application.
## Mindset Shift
If you are coming from JavaScript, this package is usually quite surprising at first. Why not just call `Math.random()` and get random floats whenever you want? Well, all Elm functions have this “same input, same output” guarantee. That is part of what makes Elm so reliable and easy to test! But if we could generate random values anytime we want, we would have to throw that guarantee out.
So instead, we create a `Generator` and hand it to the Elm runtime system to do the dirty work of generating values. We get to keep our guarantees _and_ we get random values. Great! And once people become familiar with generators, they often report that it is _easier_ than the traditional imperative APIs for most cases. For example, jump to the docs for [`Random.map4`](Random#map4) for an example of generating random [quadtrees](https://en.wikipedia.org/wiki/Quadtree) and think about what it would look like to do that in JavaScript!
Point is, this library takes some learning, but we really think it is worth it. So hang in there, and do not hesitate to ask for help on [Slack](https://elmlang.herokuapp.com/) or [Discourse](https://discourse.elm-lang.org/)!
## Future Plans
There are a ton of useful helper functions in the [`elm-community/random-extra`][extra] package. Do you need random `String` values? Random dictionaries? Etc.
We will probably do an API review and merge the results into this package someday. Not sure when, but it would be kind of nice to have it all in one place. But in the meantime, just do `elm install elm-community/random-extra` if you need stuff from there!
[extra]: https://package.elm-lang.org/packages/elm-community/random-extra/latest