Code Monkey home page Code Monkey logo

random's People

Contributors

andremw avatar evancz avatar harrysarson avatar rgrempel avatar sidneynemzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

random's Issues

Rename Generator to Random

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.

lazy+andThen+map3 overflowing the stack

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)

recursion

Switching the recursive generators (Plus3 for Plus2) or changing the seed to 1 shows non-failing case.

Provide access to construct a generator from `Seed -> (a, Seed)`

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.

Another broken link, this one in Random module docs

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. :)

`Random.float` lacks sanitization of input and emits invalid numbers when bounds are swapped

Random.int contains a sanitization step at:

random/src/Random.elm

Lines 81 to 85 in c1c9da4

( lo, hi ) =
if a < b then
( a, b )
else
( b, a )

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:

random/src/Random.elm

Lines 180 to 182 in c1c9da4

-- Scale it into our range
range =
abs (b - a)
There is no information in the documentation warning of this (otherwise silent) error, and it violates the principle of least surprise.

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.

Feature Request: Random.generateTask

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
            )

How can I encode a seed as JSON?

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!

Request: use better source of entropy

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.