Skip to content

optionally parametrize algorithms by a rand.Rand instead of relying on the default#17

Open
mbivert-ipsos wants to merge 1 commit intoccssmnn:masterfrom
synthesio:random-generator-in-settings
Open

optionally parametrize algorithms by a rand.Rand instead of relying on the default#17
mbivert-ipsos wants to merge 1 commit intoccssmnn:masterfrom
synthesio:random-generator-in-settings

Conversation

@mbivert-ipsos
Copy link

since Go 1.24, rand.Seed() is a no-op. fixing the seed was convenient for code expecting predictable output (e.g. tests). supplying a rand.Rand with a specific Seed allows to recover the pre-1.24 behavior.

anneal.go Outdated
Comment on lines 47 to 51
Settings
// Random generator, overriding math/rand's default rand.Source when specified.
// This is in particular useful for tests predictibility, as since Go 1.24,
// rand.Seed() is a no-op; but still as effective with non-default Sources.
*rand.Rand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have a Settings here, so you have Settings.Rand ? as you defined Rand in Settings

no ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, trailing from the first implementation, fixed

@mbivert-ipsos mbivert-ipsos force-pushed the random-generator-in-settings branch 3 times, most recently from 227beed to 2b0cafe Compare June 24, 2025 12:50
@mbivert-ipsos mbivert-ipsos marked this pull request as draft June 24, 2025 12:55
anneal.go Outdated
Comment on lines 58 to 60
if s.Rand == nil {
s.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not calling Setting.Init ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erf, again, trailing from first implementation, fixed

…n the default

since Go 1.24, rand.Seed() is a no-op. fixing the seed was convenient
for code expecting predictable output (e.g. tests). supplying a rand.Rand
with a specific Seed allows to recover the pre-1.24 behavior.
@mbivert-ipsos mbivert-ipsos force-pushed the random-generator-in-settings branch from 2b0cafe to a08f39e Compare June 24, 2025 12:58
// Random generator, overriding math/rand's default rand.Source when specified.
// This is in particular useful for tests predictibility, as since Go 1.24,
// rand.Seed() is a no-op; but still as effective with non-default Sources.
*rand.Rand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use an interface

This way rand could either math/rand, math/rand/v2, or anything that user needs, including something that is not random, that could be useful for test purpose

it's important to validate in tests that math/rand.Rand and math/rand/v2.Rand satisfies this interface

@mbivert-ipsos mbivert-ipsos marked this pull request as ready for review June 24, 2025 15:17
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

Successfully merging this pull request may close these issues.

2 participants