-
-
Notifications
You must be signed in to change notification settings - Fork 406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature srandom - A seeded random number #2252
Conversation
A function generating seeded random numbers, allowing for sorting lists in a random, but predictively fashion.
I like this idea but I worry that slight changes to the execution order of function calls (such as more calls from more data, or maybe a patch in dataview shifting around execution order when executing queries) will make this frustrating to use. As an alternative, maybe just a random hash function? One that takes |
hm. I consider these algo's to be too costly for generating pseudo randomness. just for reference, i've done some tech demos before, that are basically based on this prandom gen concept: const generator = (seed) => {
const func = () => {
seed *= 1103515245 + 12345;
seed--;
return (seed %= Number.MAX_SAFE_INTEGER) / Number.MAX_SAFE_INTEGER;
};
func();
return func;
}
const rand = generator(1337);
console.log(rand(), rand(), rand()); // 0.37070590492482164 0.860457525371235 0.2702827315720024
@blacksmithgu well.. that's literally the functionality pseudo randomness guarantees. it's a feature. |
I'm not quite sure I understand your concern regarding the execution order due to this being a random function. But then again the random number generated for a given row will change if the execution order for the rows change. On the other hand, using just a hash function like the Should we close this PR, and create another for that hash implementation and documentation of it? |
hm. technically @blacksmithgu has a point tho.
but.. since we are talking about dql here, how else could it be implemented then? |
How did you get that to return different values? In all of my tests, and even the unit tests has a very similar setup where it consistently returned the same values over and over again. |
I'm going to build another PR using just a hash function, and it's going to be a slightly simpler version of the I did the following https://jsperf.app/bowuze to compare some variation of a hash function. Feel free to extend with some other variants, but as it stands currently the If (or when) the other PR is created, and no-one opposes to it this PR can be closed down, and the other one integrated into the master. :-D |
na don't worry about it. I wasn't talking about true randomness. I was just referring to the way this keeps track of nounces |
I've merged the related |
The linked PR has been pushed into master, with similar, but simpler, logic, so we can then close down this PR. |
A function generating seeded random numbers, allowing for sorting lists in a random, but predictively fashion.
This function allows for queries to return random items when combined with sorting on the random number, and using
LIMIT
to get the number of random results you want. Due to the seed, which could be a date likeYYYY-MM-DD
, the random result will be consistent as long as the source list or the date actually changes.A simple example query to get three random links:
I originally hoped to put the expression directly into
SORT ...
but it requires a field, so it seems I have to use the intermediateFLATTEN ... as randomValue
to get it to be that field. But it do work consistently.