-
Notifications
You must be signed in to change notification settings - Fork 7k
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
No mechanism to use seeded random generation #3289
Comments
Hi @hugoferreira! If you're wanting that level of control I think you'd be better off with a mixin or specialized library for randomizing things. |
Is this open to change? It's not the end of the world if not, but I think seeded random number generators would be a nice addition to Lodash too, especially as it already has an unseeded RNG. There's a library called The basic idea with The Lodash |
Hi @carlsmith! For folks who want this I'm happy to punt to another specialized package. |
Cool. Thanks for considering it. |
This would be super useful for me and I'm happy to make the PR. There are two architectures that come to mind:
Let me know if any other design makes sense to you all! |
The tests folder appears to be missing from master, should I wait until the next version of lodash? |
@yoiang The |
@falsyvalues yah, I'd read! :) So should I wait for v5? |
@yoiang I could really use this feature for testing application methods that rely heavily on lodash. The second implementation would make much more sense in this scenario as I don't want to have to pass an additional parameter throughout my application source just for the purposes of testing. |
The easiest way for me to get my desired behavior (in my main JS file of interest) was as follows: /* Top of script */
Math.seedrandom("my-random-seed");
_ = _.runInContext();
/* Rest of script */ |
For those that don't know what what this, As that project documents, Having a seed option for Lodash random would save adding a new dependency, but I realize this is a less common use of In my case, I want a bunch of different customer jobs to run at random-but-stable times to spread the load out. By generating a random number with the customer name as a seed, all the customer names are converted into random-but-stable numbers based on the customer name. The stability is nice, so if see there's a slow job that happening every day at 5:23 AM, I can track down that the job belongs to "customer X". |
@markstos , why do not you use a hash function in your case to map customers to evenly distributed numbers? |
@rriemann That would work, too. The Jenkins project uses the hashing approach to allow you to run jobs "every 30 minutes based on a hash of the project name". |
I would also very much want a seed for lodash random, shuffle, etc. I want it for testing and reproducibility. Especially now that data science is becoming a more popular use case in Javascript (my use case). I was very surprised seed is not a parameter to the random number methods. I think I would rather have no random methods at all in lodash, than to have them w/o seeds. |
I sympathize with your opinion, in the sense that people look for libraries such as However, I think the main issue why @jdalton regards such functionality as out-of-scope is because JS/ES does not have a built-in seedable PRNG. Either The best possible scenario would be for JS/ES to provide a standard way of seeding its PRNG programmatically, but I don't believe this is even on their horizon. Node supports the |
I agree. This seems like the best design. And then from there it would be easy to add a very rudimentary PRNG with seeding |
I am using seedrandom and lodash together. I made sure to include seedrandom before lodash when I manually include my script tags and I initialize the seed value too. It works like a charm! <!-- Specifically include seedrandom before lodash so lodash uses the seededd ranom -->
<script src="thirdjs/seedrandom.js"></script>
<script>
Math.seedrandom("dev")
</script>
<script src="thirdjs/lodash.js"></script>
<script>
console.log('run this again to see _.sample chooses the same number every time')
console.log('seed', _.sample([1, 2, 3, 4, 5, 6, 7, 8, 9]);
</script> |
@geluso @stefanuddenberg suggested this functionality above. While it works just great it does require intimate knowledge of what lodash uses internally, should lodash ever change the internal behavior of this technique would fail without notice. On the other hand APIs and interfaces are characterized as defining interactions between systems. Capturing this requirement as a method or in a signature sets up expectations, is commonly tracked by semantic versioning, can have tools report changes, etc. |
Ah, I see. I must have scrolled through @stefanuddenberg's response too. Thanks for pointing that out. |
For testing purposes you can use importing module with
import seedrandom from "seedrandom";
seedrandom("seed", { global: true });
import "./seed.js";
import _ from "lodash";
_.shuffle([1, 2, 3]); // Produces deterministic shufflings! |
The code provided by @alexamy didn't work for me. To use seeded random generation for Lodash, I needed to do this: import seedrandom from "seedrandom";
import _lodash from "lodash"; // Temporary name for Lodash
seedrandom("seed", { global: true });
const _ = _lodash.runInContext();
_.shuffle([1, 2, 3]); // Always the same I read about this here: davidbau/seedrandom#32 (comment) |
Here's a handy typescript function, based on the smart people commenting above 👌 // shuffleSeeded.ts
import seedrandom from 'seedrandom';
import _lodash from 'lodash';
export function shuffleSeeded<T>(array: T[], seed: string): T[] {
seedrandom(seed, { global: true });
const _ = _lodash.runInContext();
return _.shuffle(array);
} |
@benwinding 's answer above has a flaw in setting the seed every call, e.g.
At A the global seed has begun at Instead, I propose the following:
This creates a lodash instance which uses a
|
Currently, methods that expose random behaviour, like
sample
, use the internalMath.random()
function, which is unseeded. Not being able to optionally enforce deterministic behaviour in a functional setting is... odd. I would like to suggest the discussion over possible enhancements and alternatives to implement such feature, amongst which:Math.random()
to a lodash random number generator;sample
to set the seed;sample
that provides a generator;The text was updated successfully, but these errors were encountered: