Examples Page Needs Random Example

240 views
Skip to first unread message

Craig Stuntz

unread,
Feb 18, 2015, 4:33:01 PM2/18/15
to elm-d...@googlegroups.com
I'm currently working through the Elm examples from "7 More Languages in 7 Weeks," and I'm converting this (0.13) game from the book to 0.14:


I can make this work, but I want to be following the "best practices" for 0.14 as it is today.

This page:


...has no good Random example. A couple of the games uses Random, but they don't do it well (fixed seed) and you have to know to look for them. New users won't find the Random module at all obvious. The documentation for Random handwaves over how to get a seed:


Joe Collard has an example which at least works:


New users might not find this code clear, and it's not clear to me if this is the best way for 0.14, but it sure beats a fixed seed. 

I think that for the short term there needs to be a good example on the Examples page. I hope this can be done fairly soon. 

For the long term there needs to be a way to get a random seed which is easier for users and which has more entropy than a timestamp. 

I discussed this on Twitter with Evan today, and he suggested posting here.


Thanks!

Craig

Hassan Hayat

unread,
Feb 18, 2015, 4:51:21 PM2/18/15
to elm-d...@googlegroups.com
Chance has it that I have just finished writing a tutorial on writing your own random generators. I know it doesn't address all your concern, but hopefully it has some useful info.

https://github.com/TheSeamau5/elm-check/wiki/Guide-to-Writing-Your-Own-Random-Generators

This tutorial is aimed at working with elm-check (elm library for property-based testing) but this wiki entry only mentions testing in passing. It deals more with creating your own generators and mixing and matching functions. 

When it comes to seeding, unfortunately, as of now, there's no best practices and the current random generation is far from cryptographically secure. I hope work will be done to improve it as this is an issue that has come up several times since the Random library was published. And aside from cryptographic security, even a library such as elm-check stands to win by being able to generate values with a more uniform distribution. 

As an example of using the current time as a seed can be found in elm-check.

This is the "check" function that checks for properties:

check : List Property -> Seed -> TestOutput
check properties seed = map (\f -> f seed) properties

Don't worry about what it does. Just notice the "seed" as the second input.

And this is the "continuousCheckEvery" which runs "check" every (some given time interval) and uses the current time as the seed.

continuousCheckEvery : Time -> List Property -> Signal TestOutput
continuousCheckEvery time properties =
  Signal.foldp mergeTestOutputs []
    (Signal.map ((check properties) << initialSeed << round) (every time))

Don't worry about the foldp and merging of outputs, look at the bottom.

What it does. It calls "every time", which, if you replace time by second would output the current unix time (as a float) every second. We then Signal.map round it (because every time is a signal of floats, so we need to round them). The we pass that to initial seed (by calling Signal.map) and the pass it to (check properties) again, using Signal.map.

The "<<" operator is there for composition and so I don't end up with a while bunch of Signal.map calls. Basically i pre-compose the entire chain first and then Signal.map it onto every time. 

I know, currently, bad bad bad.

So, yeah +1 on crypto secure pseudo random generation.

Sebastian Graf

unread,
Feb 18, 2015, 6:25:15 PM2/18/15
to elm-d...@googlegroups.com
I'm concerned that this fails to change the seed when there has no whole second passed since the last sample/program start. Isn't there any better way? E.g. seeding from JavaScript's random() function or something similar.

Hassan Hayat

unread,
Feb 18, 2015, 6:43:50 PM2/18/15
to elm-d...@googlegroups.com
I wonder, what if the seed itself is produced by a random generator like "int". Would that additional level improve the overall random generation?

Craig Stuntz

unread,
Feb 18, 2015, 7:55:25 PM2/18/15
to elm-d...@googlegroups.com

int requires a Seed, so you can't use int to get the initial Seed. Also, running a pseudorandom generator twice doesn't make its output more random. You want a truly random seed, and the best place to get that is from hardware.

Here's a (slightly old) article about randomness in the browser:

http://bocoup.com/weblog/random-numbers/

One could probably use the FFI to do some of those techniques?

Craig


--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/ilDp3-ekJ98/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

unread,
Feb 18, 2015, 10:27:04 PM2/18/15
to elm-d...@googlegroups.com
Yes, you could. In your Elm program,

port aRandomNumber : Int

Declaring a port without a definition implies it's an incoming port. So, in the compiled JS, find the line with Elm.fullscreen or whatever and pass a second argument (the first is your program):

{aRandomNumber: Math.floor((Math.random() - 0.5) * 4294967295)}

Generating a random number is JS is a slightly more complicated topic, but this is good enough to start with.

I have said before that we should have a random seed or a random integer available. A random seed is more type-correct and less likely to be abused, but a random integer is reproducible.

Jeff Smits

unread,
Feb 19, 2015, 4:05:06 PM2/19/15
to elm-discuss
I just wrote this StackOverflow answer, and figured it's something to share here too.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages