elm / random Goto Github PK
View Code? Open in Web Editor NEWGenerate random values in Elm
Home Page: https://package.elm-lang.org/packages/elm/random/latest/
License: BSD 3-Clause "New" or "Revised" License
Generate random values in Elm
Home Page: https://package.elm-lang.org/packages/elm/random/latest/
License: BSD 3-Clause "New" or "Revised" License
https://package.elm-lang.org/packages/elm/random/1.0.0/Random
Docs for the Random.lazy function
This is a pretty subtle issue, so I recommend reading more about it here!
looks like it presently points to https://elm-lang.org/hints/0.19.0/bad-recursion which offers a 404, but new url for target appears to be here at https://elm-lang.org/0.19.0/bad-recursion .
Also if there's better places to report broken docs links than in the github, just lemme know for future reference and I'll have learnt a new thing, thank you kindly folks. :)
This commit fixed a broken link: a1eae05 . This was back in 2019. It is now 2021 The version of the docs at https://package.elm-lang.org/packages/elm/random/latest/Random is still broken
The example link in README (https://elm-lang.org/examples/random) is broken
I noticed that Elm/Random has no way to generate a random value as a Task. I’m not sure if this is intended.
Currently, I am trying to build a chat with Jsonstore at the backend.
If I now want to create a new chatroom, I need to first generate a number and then create the room.
createRoom : Task Error (Maybe Room)
createRoom =
Random.generateTask Random.int 0 Random.maxInt
|> Task.andThen insertNewRoom
sadly, Random has now task version of gernerate and therefore i need to instead split up the task into two messages.
update : Msg -> Model -> (Model,Cmd Msg)
update msg model =
case msg of
PressedNewRoomButton ->
( model
, Random.generate Random.int 0 Random.maxInt
)
GeneratedRoomId int ->
( model
, insertNewRoom int
|> Task.attempt Sync
)
Basing randomness on current time is very low entropy, at most 1000 unique values available per second, shared across all browsers in the world. Birthday paradox says 38 browsers loading the site in the same second have a 50% chance of at least one collision.
Can we please use WebCrypto.getRandomValues()
for seeding the PRNG?
People generally assume two browsers fetching random values at the same time to get different values.
In Random we have:
type Generator a =
Generator (Seed -> (a, Seed))
So if I have a way of doing Seed -> (a, Seed))
, I should be able to make a Generator a
.
Well not exactly.
I can do:
fromStep : (Seed -> ( a, Seed )) -> Generator a
fromStep step =
Random.independentSeed |> Random.map (step >> Tuple.first)
The problem here is independantSeed
goes through the extra work of creating a new seed, and then we throw out the seed after the generator is used.
There’s no way to use a seed and step, (generating a value) and returning the value and the next seed.
Random.int
contains a sanitization step at:
Lines 81 to 85 in c1c9da4
As such, Random.int 100 -100
produces values in the range [-100, 100]
Random.float
contains no such step, and Random.float 100 -100
produces values in the range [100, 300]
due to this, which assumes the bounds were passed in the correct order:
Lines 180 to 182 in c1c9da4
Either the same sanitization step should be added to Random.float
or at the very least a prominent warning should be added to the documentation.
Over on discourse, someone was asking about the best practices for using this library and had apparently missed what we tried to convey about generators and deemphasizing seeds. Now maybe that's just part of the learning process but the idea occurred to me to rename Generator a
to Random a
. The comment got six hearts so some people think it's a good idea.
The benefit is that by following the "module built around a data structure of the same name" paradigm, we would really enforce that Random a
is where it's at. And Random Int
or Random User
read quite nicely.
The large downside of course is that it's a major change, and would require a lot of tedious updates (including in elm-test
and then other people's tests). Also, I can refer to generators as nouns pretty easily ("a way to combine two generators"), but "random" is an adjective so we'd need to say "a way to combine two Random
values" or similar.
It says:
Now look at this working example to see a
Generator
used in an application.
Probably better to link to https://guide.elm-lang.org/effects/random.html until the examples on the Elm website can be brought back to life.
Hey there! Hope you're doing well! I'm building a browser game where many clients are synced up and I need to share seeds amongst clients;
I can get a Random.Seed
into elm no problem by using Random.initialSeed
; but once I have a seed I can't serialize it back into JSON; Is there any way to encode a Seed? I simply need either a Random.Seed -> Int
or a Random.Seed -> Json.Encode.Value
Does this exist; or would it be possible to add? I'm a bit stuck on this one 😬
Thanks for your help!
Use of these three (or maybe two, I'm not sure about map3
) results in "too much recursion."
Ellie with a SSCCE ➡️ https://ellie-app.com/4Kv76dc7Hkpa1
type Expr
= Int_ Int
| Plus2 Expr Expr
| Plus3 Expr Expr Expr
exprGenerator : Generator Expr
exprGenerator =
Random.uniform
(Random.map Int_ (Random.int Random.minInt Random.maxInt))
--[ Random.map2 Plus2 lazyExprGenerator lazyExprGenerator
[ Random.map3 Plus3 lazyExprGenerator lazyExprGenerator lazyExprGenerator
]
|> Random.andThen identity
lazyExprGenerator : Generator Expr
lazyExprGenerator =
Random.lazy (\() -> exprGenerator)
Switching the recursive generators (Plus3 for Plus2) or changing the seed to 1 shows non-failing case.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.