Skip to content
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

Allow for a custom random seed when sampling #512

Open
bramvandewalle opened this issue Aug 12, 2024 · 0 comments
Open

Allow for a custom random seed when sampling #512

bramvandewalle opened this issue Aug 12, 2024 · 0 comments

Comments

@bramvandewalle
Copy link

Context

In one of our projects, we have a collection of objects. In case the size of a collection is bigger than a threshold, we will sample the collection. We have to sample it in a deterministic way so that the result is the same when we sample the full collection again later on.

For that, we currently do something like:

func sampleObjects(objects []*pkg.Object, seed string, size int) []*pkg.Object {
	rand.Seed(getHash(seed)) //nolint: deprecated
	return lo.Samples(objects, size)
}

However, it is not ideal that we have to use a deprecated way of seeding rand.

Request

Hence, it would be great if instead, we would be able to do something like:

func sampleObjects(objects []*pkg.Object, seed string, size int) []*pkg.Object {
	r := rand.New(rand.NewSource(getHash(seed)))
	return lo.SamplesWithRand(objects, size, r)
}

or

func sampleObjects(objects []*pkg.Object, seed string, size int) []*pkg.Object {
	hashSeed := getHash(seed)
	return lo.SamplesWithSeed(objects, size, hashSeed)
}
@bramvandewalle bramvandewalle changed the title Allow for a custom random seed Allow for a custom random seed when sampling Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant