News for hackers. Stuff that matters.
Re: by nonethewiser on Monday May 22, 2023 @10:11PM (#36038985)
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.

Re: by damienwebdev on Tuesday May 23, 2023 @08:25AM (#36043288)
I'm one of the maintainers of Faker, so hopefully I can help remedy any situation we inadvertently created.

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.

Re: by nonethewiser on Tuesday May 23, 2023 @10:08PM (#36053232)
Oh shit, youre right. Thanks for that. I think I saw that method but didnt realize it is implicitly random (like an idiot)
Re: by jasonjmcghee on Monday May 22, 2023 @10:19PM (#36039035)
I think seeded randomness is wildly underused. It's much more often the case I was deterministic randomness rather than based on my system clock.

It's nice to see aimless.js support it.

Crazy to me that `Math.random` doesn't accept a seed as an optional param.

Re: by quickthrower2 on Tuesday May 23, 2023 @12:16AM (#36039762)
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.

Re: by explaininjs on Tuesday May 23, 2023 @06:32AM (#36042277)
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 gmac on Tuesday May 23, 2023 @02:59AM (#36040852)
Agreed. That’s why I did this (although the Mersenne Twister is probably a rather outdated way to go): https://github.com/jawj/mtwist
Re: by sebstefan on Tuesday May 23, 2023 @04:29AM (#36041525)
I was really surprised when I discovered JS didn't have seeded randomness

I had to make one myself and I never thought it'd be something I'd ever need to do

Re: by paddw on Monday May 22, 2023 @11:14PM (#36039357)
Cool project! Might be nice to include a random permutation method.
Re: by shhsshs on Monday May 22, 2023 @11:50PM (#36039593)

    const randomPermutation = arr => intSequence(0, arr.length - 1).map(i => arr[i])
    console.log(randomPermutation(['a', 'b', 'c'])
Re: by chriscavs on Tuesday May 23, 2023 @11:21AM (#36045593)
I believe sequence will accomplish what you're looking for here

const randomSeq = sequence([1,2,3]) // could return [3,1,2], [2,3,1], etc.

Re: by tantalor on Monday May 22, 2023 @11:16PM (#36039377)
intSequence(min, max, engine) Returns an array with all integers between min and max in random order.
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.
Re: by EarthLaunch on Monday May 22, 2023 @11:32PM (#36039473)
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.

Re: by quickthrower2 on Tuesday May 23, 2023 @12:14AM (#36039746)
I was thinking "if there is one feature JS doesn't have, it is a missing library"
Re: by chriscavs on Tuesday May 23, 2023 @11:16AM (#36045507)
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.
Re: by EarthLaunch on Monday May 22, 2023 @11:21PM (#36039410)
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
0: https://news.ycombinator.com/item?id=34742505

1: https://news.ycombinator.com/item?id=34742985

2: https://github.com/Suncapped/babs

Re: by chriscavs on Tuesday May 23, 2023 @11:13AM (#36045465)
thanks for the comment, i'm happy you like it!
Re: by vore on Tuesday May 23, 2023 @12:29AM (#36039837)
This is not a uniform distribution:

  const randIntRange = (i, j, engine = defaultEngine) => {
    const min = Math.ceil(i)
    const max = Math.floor(j)

    return Math.floor(engine() * (max - min + 1)) + min
  }
If you assume engine() outputs, say, 64 bits of entropy, any number range that is not an even factor of 2^64 will exhibit left bias.

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.

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.
Re: by chriscavs on Tuesday May 23, 2023 @10:56AM (#36045240)
thanks for bringing this to my attention, I will be sure to check this out
Re: by chriscavs on Tuesday May 23, 2023 @10:52AM (#36045175)
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
Re: by winrid on Tuesday May 23, 2023 @03:38AM (#36041152)
Whether or not my code will run is all the randomness I need.
Re: by nailer on Tuesday May 23, 2023 @08:41AM (#36043424)
> The default engine uses crypto.getrandomvalues() when available, with a fallback of Math.random().

Thumbs up. Carry on.