I was using faker in our codebase at work to generate some random data. I wanted to pick one of a few status codes. I was surprised to see that faker.random was deprecated and the random sample was moved into different datatypes. But the array datatype was deprecated so now there is no random sample from array in faker, seemingly. They say "Use your own function to build complex arrays." Isn't that what faker is all about?
Maybe there is good reason for this. I'm not real familiar with this space. It's not so hard to implement something yourself. But I suspect the description of "missing js randomness library" may have some merit.
I believe is what you're looking for. The issue, as I recall, is that all of Faker is random, so what does it mean to have a `random` module namespace. We moved it out of a hope for improved clarity.
Yes, the obvious case is machine learning, so that you can make changes and run your program again, and it uses the same batches as so forth.
Another case are unit tests with random-generated cases. At a minimum you want to know the seed of the last run and have that reported. On CI system you would want it to be deterministic.
Where you want there not to be a seed is in crypto, or possibly a game like poker or even tetris.
I’d argue that in poker you must use seeded randomness — but everyone has to contribute to the random “pool”. This is the only way I know of to have effectively random values with zero trust.
Re:
by re on Monday May 22, 2023 @11:21PM
(#36039403)
"The missing JS randomness library" is a provocative subtitle. :) Have you compared your project to Random.js? https://www.npmjs.com/package/random-js There's also Rando.js but that project also appears to use a biased algorithm.
Thanks for mentioning others. The main thing I love about OP's library is the simple, clear naming. A lot of complexity has been solved for me.
Looking at random-js, the barrier to entry is higher. I've looked at randomness libraries and that's the feeling they gave me: That I would need to learn how randomness works, rather than just calling a function for what I want; a random int, a weighted random. Cmd+F "weighted" doesn't find anything on both of those projects' readmes.
There's a tradeoff for simplicity, but in many cases I'll take it. This is the iPhone of randomness and that's what I needed to feel comfortable using it rather than rolling my own.
Thanks for the comment! Yes it is a bit of a provocative title hahaha. i don't believe my library can be compared to random-js, since the goals of each are very different. random-js wants to provide the "engine" that replaces Math.random. I want to provide a library of utilities that are engine-agnostic. you can pull in random-js and use their engines to power aimless.js, as an example.
A while back there was a post about a randomness bug in Asheron's Call, the "Wi flag" [0]. I posted a comment there and was offered helpful answers about doing weighted randomness [1].
This library seems to solve that problem and half a dozen others! Like: Reading code with functions named for their purpose is better than a string of arithmetic operations in most cases. Thank you for sharing this! Will use in my game! [2]
const weightedDiceRoll =
weighted(
[1,2,3,4,5,6],
[1,1,1,1,1,10]
)
// will return 6 much more often than the other options
Re:
by vore on Tuesday May 23, 2023 @02:12AM
(#36040539)
Additionally, the shuffle function (sequence.js) has quadratic complexity due to repeated copying of the input array. You could get a vast performance improvement by copying the input array and running the standard Fisher–Yates shuffle over it: it will both run in linear time and will not thrash cache as much.
Thank you for the comment! I am aware of this issue, and am working to address in v1.1.0. The context you provide here is very helpful for me. I've opened an issue on github for this: https://github.com/ChrisCavs/aimless.js/issues/2
Maybe there is good reason for this. I'm not real familiar with this space. It's not so hard to implement something yourself. But I suspect the description of "missing js randomness library" may have some merit.
https://fakerjs.dev/api/helpers.html#arrayelement
I believe is what you're looking for. The issue, as I recall, is that all of Faker is random, so what does it mean to have a `random` module namespace. We moved it out of a hope for improved clarity.
It's nice to see aimless.js support it.
Crazy to me that `Math.random` doesn't accept a seed as an optional param.
Another case are unit tests with random-generated cases. At a minimum you want to know the seed of the last run and have that reported. On CI system you would want it to be deterministic.
Where you want there not to be a seed is in crypto, or possibly a game like poker or even tetris.
I had to make one myself and I never thought it'd be something I'd ever need to do
const randomSeq = sequence([1,2,3]) // could return [3,1,2], [2,3,1], etc.
Looking at random-js, the barrier to entry is higher. I've looked at randomness libraries and that's the feeling they gave me: That I would need to learn how randomness works, rather than just calling a function for what I want; a random int, a weighted random. Cmd+F "weighted" doesn't find anything on both of those projects' readmes.
There's a tradeoff for simplicity, but in many cases I'll take it. This is the iPhone of randomness and that's what I needed to feel comfortable using it rather than rolling my own.
This library seems to solve that problem and half a dozen others! Like: Reading code with functions named for their purpose is better than a string of arithmetic operations in most cases. Thank you for sharing this! Will use in my game! [2]
0: https://news.ycombinator.com/item?id=347425051: https://news.ycombinator.com/item?id=34742985
2: https://github.com/Suncapped/babs
The only correct way to do this is to do rejection sampling: generate a number x with log2(max - min) bits and if x >= max - min retry.
Thumbs up. Carry on.