tc39-transfer / proposal-random-functions

Proposal to add a Random namespace and several additional convenience functions for using randomness.
https://tc39-transfer.github.io/proposal-random-functions/
MIT License
3 stars 1 forks source link

more prior art #8

Open michaelficarra opened 1 month ago

michaelficarra commented 1 month ago

JSVerify is another property-based (generative) testing library for JavaScript. Its documentation lists its Arbitrary instances starting here: https://github.com/jsverify/jsverify#primitive-arbitraries

I think it would be a good (necessary but not sufficient) evaluation criteria to ensure that the Arbitrary instances provided by these generative testing libraries could be relatively easily defined in terms of the functions provided in this proposal.

edit: here's another one: fast-check

tabatkins commented 1 month ago

Yup, between "random number in a range", "random int in a range", and "random value from a list", it looks like all the jsverify primitives are covered. And same with fast-check.

michaelficarra commented 1 month ago

I dunno about that. For example, it's non-trivial to get a good distribution of BigInts. You'd have to write something like this today:

BigInt(function(){
  let s = '';
  do { s += Math.floor(Math.random()*10); } while(Math.random() < 0.9);
  return s;
}())

There's plenty of ways we could make this better:

And there's plenty of other tricky things. There's lots of design choices to be made with random strings. It's hard to pick a Date (or Temporal.PlainDateTime in modern code) that's bounded by a year/month/day. While the web platform has crypto.randomUUID(), it doesn't have one that would work with a seeded PRNG.

bakkot commented 1 month ago

I don't think those are problems this proposal should aim to solve, personally.

tabatkins commented 1 month ago

There is no uniform distribution over the integers, but as long as you have a range that you do care about, you can get one easily. For anything else, what distribution you want will depend on your use-case pretty severely.

michaelficarra commented 1 month ago

Yes, use cases will vary quite a bit, but I think just a few very common (parameterisable) distributions will cover the large majority of non-uniform use cases. As you know, there's precedent for this in Java, Python, and probably others if I went looking.

This is getting a bit off-topic from the original post. Maybe I should move it to #6?