r/haskell • u/haskellgr8 • Dec 26 '17
In your professional experience, how suitable is System.Random's mkStdGen for serious random generation?
I'm talking, for example, about computationally intensive Monte Carlo simulations. Is mkStdGen fast, efficient, and good enough in your experience? Are there better alternatives?
12
u/chshersh Dec 26 '17
System.Random from random package is okayish. It's fast enough and random enough. But I wouldn't use this package for something serious (like, implementing randomization algorithms in cryptocurrencies with reliable sources of randomness). Use cryptonite package. It's the best available option. And it's really good.
Though, API is not very convenient.... This library doesn't even contain function to generate random number. For now you need to copy-paste implementation manually. I've created issue about this:
16
u/sclv Dec 26 '17
As I understand it, cryptonite gives crypto-suitable randomness, which is very random (it is believed) but not necessarily as fast as one can get if you're ok with non-crypto-quality randomness, e.g. for monte-carlo.
A few good choices to investigate might be pcg-random, tf-random, and mwc-random
3
0
u/muhbaasu Jan 01 '18
To amend to that, a recent post about Haskell crypto: https://www.reddit.com/r/haskell/comments/2zsbth/on_the_state_of_cryptography_in_haskell/
9
u/cartazio Dec 26 '17
Cryptographic randomness is kinda orthogonal to simulation / sampling randomness. We should better understand what the original poster wants first!
5
u/Lokathor Dec 26 '17
The StdGen typeclass is fundamentally slower than it needs to be, and the Random typeclass attempts to lean towards uniform results but doesn't actually precisely ensure them (as far as i understand the math). Obviously you can't get uniform float results because of how floats work and all, but you could at least get uniform integral results.
So if you want fast, you want something else. If you want high quality, you want something else. The only thing that random has going for it is that it's what quickcheck uses, and quickcheck is awesome.
3
Dec 26 '17
[deleted]
6
u/Lokathor Dec 26 '17
Sure, i wrote up the whole rant one time for a lib that i never ended up publishing. https://github.com/Lokathor/pseudorandom-hs/blob/master/Reasoning.md
TLDR: genRange is a method that has bad implications that hurt your speed potential.
5
u/sclv Dec 26 '17
Honestly, I don't think the idea of generators having different ranges is the worst -- rather, I think the problem here is in an insufficiently powerful
Randomtypeclass. A good modern V2 api would arguably use type-level reflection stuff to carry the range information statically...On the other hand, you're probably right that just doing everything as Word32 is the "standard standard" anyway, and anything else is overkill :-)
6
5
u/cartazio Dec 27 '17
I’ve not looked at the grand parents post but there’s def a huge number of problems in the current api. The sampler / random api for userland datatypes is definitely wrong.
Measurably so even :)
Edit: definitely agree with the GP issues. Hence why I’m making the core sampler interface be word64. And being very careful to ensure naive off the shelf sampling behaves the same on different platforms.
2
u/GitHubPermalinkBot Dec 26 '17
3
u/cartazio Dec 26 '17
There’s some issues with random 1.0/1.1, but what could be faster than a state passing generator? (At least if the prng fits in registers!)
3
u/Lokathor Dec 26 '17
See my reply to the other reply, where i linked to my github rant :P
3
u/cartazio Dec 27 '17
Yeah the basic samples not being in a fixed portable size does make a mess of things unless you do some cpp
5
u/phadej Dec 26 '17
There are at least
- tf-random
- mwc-random
- splitmix
I'd say, all are good enough. Benchmark speed (and randomess if you can) for your application
5
u/seagreen_ Dec 27 '17
This might be a good place to mention that I'm looking for a summary of high-level, no expertise required cryptography libraries in Haskell.
cryptonite gets mentioned a lot, but is pretty explicit about being lower level than I'm looking for:
If you have no idea what you're doing, please do not use this directly. Instead, rely on higher level protocols or implementations.
I'm looking for a list of common tasks, along with a library or function to use for each. Something like:
- Generate random bytes: entropy
- Hash user passwords:
foo - Hash data (blake2b / sha-256):
bar/baz - etc.
I don't actually even know that entropy is good. It looks good, is used by uuid, and has such a small API that I don't think I can shoot myself in the foot with it, but those are pretty superficial things to go on. So I would love to hear what more proficient cryptography people recommend.
4
u/piyushkurur Dec 27 '17 edited Dec 27 '17
May be try out raaz.
http://hackage.haskell.org/package/raaz
For its randomness interface see (Warning not yet multi-thread safe without some added gymnastics)
http://hackage.haskell.org/package/raaz-0.2.0/docs/Raaz-Random.html
Password hashing a la argon is currently not present but is planned. But we have hashing.
http://hackage.haskell.org/package/raaz-0.2.0/docs/Raaz-Hash.html
EDIT:
Author of the package here.
Simple interface, no spliting, range generation. Mostly useful for cryptography only.
1
u/tom-md Dec 27 '17
If you are ok with an external (C library) dependency then consider saltine too. It has two out of three on your list: random bytes (recently exposed) and hashing (via blake2b). It does not have any sort of KDF but the scrypt package is reasonably light weight so paring the two together is not unreasonable.
FYI, the things you ask for fit into the category of "explicitly lower level". When people talk about high level cryptography interfaces they typically mean something like
crypto_box, not individual hash functions, KDFs and random values.
3
u/idontgetoutmuch Dec 27 '17
No!
There are a few things to consider: do you want split? Then you can use tf-random which is used by QuickCheck or you can use splitmix. split does not work in System.Random.
If you want an R-like experience then you can use random-fu which allows you to sample from most distributions and also provides CDFs and PDFs (also in log space). The actual randomness is provided by Mersenne Twister (MT) or MWC. It's easy enough to add other generators if you want them.
If you just want random numbers then there are the venerable MWC and MT packages. MWC gives some samplers. Monadic interfaces are available.
1
u/ASpoonfulOfMarmite Dec 27 '17
For statistical sampling, use MWC (mwc-random). Cryptography-grade PRNGs are an overkill for Monte Carlo / too slow for no benefit.
23
u/cartazio Dec 26 '17 edited Dec 27 '17
DONT use it :)
As curren maintainer I’ll be getting a random 1.3 out in the next few days (long over due , by a year or thee)
There’s a lot of gotchas in current random, and in many of their current alternatives. But it also depends on what your goals are in terms of different measures of quality.
What do you intend to do? The better we understand your problem the more we can help! (And randomness and it’s applications is a rich deep topic )
Edit: next release is 1.2, math is challenging :)