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

x/playground: result not cached #58324

Open
nussjustin opened this issue Feb 4, 2023 · 7 comments
Open

x/playground: result not cached #58324

nussjustin opened this issue Feb 4, 2023 · 7 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@nussjustin
Copy link
Contributor

What version of Go are you using (go version)?

1.20 on the playground

Does this issue reproduce with the latest release?

With 1.20, but not 1.19

What operating system and processor architecture are you using (go env)?

Playground

What did you do?

Ran the code from @rsc new blog post (link) multiple times.

Whole file:

// Note that this program will print one answer
// consistently on the Go playground due to caching.

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	fmt.Println(rand.Perm(37)[:10])
}

What did you expect to see?

The same result when running multiple times, as written at the top of the file:

// Note that this program will print one answer
// consistently on the Go playground due to caching.

What did you see instead?

Different results.

@gopherbot gopherbot added this to the Unreleased milestone Feb 4, 2023
@seankhliao seankhliao changed the title x/website: playground result not cached when using Go 1.20 x/playground: result not cached when using Go 1.20 Feb 4, 2023
@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed website labels Feb 4, 2023
@seankhliao
Copy link
Member

cc @@toothrot

@mrwormhole
Copy link

mrwormhole commented Feb 5, 2023

I think this is to be expected, 1.20 now automatically randomly seeds numbers for us, where in 1.19, the seed would always be 1 and print this number in each run, it is not related to cache I believe

5577006791947779410 (this number is printed in every version below 1.20) https://go.dev/play/p/qCp3lxxXQ_A?v=goprev

image

image

@nussjustin
Copy link
Contributor Author

@mrwormhole the playground is, or at least was, to my knowledge supposed to be deterministic so that results don't change over time and can be cached (even sleeps are actually cached and emulated in the browser!)

@phenpessoa
Copy link

I'm not sure what is to be expected from the playground in this regard either.
In the about section there is: In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.
So it looks like only the time is deterministic. But before go 1.20, using math.Rand was indeed also deterministic.
On the other hand, crypto/rand was never deterministic, afaik.

@findleyr
Copy link
Contributor

findleyr commented Feb 6, 2023

Thanks for the report.

Caching does indeed appear to be broken, though I don't think this is specific to 1.20. I can take a look.

@findleyr findleyr self-assigned this Feb 6, 2023
@findleyr findleyr changed the title x/playground: result not cached when using Go 1.20 x/playground: result not cached Feb 6, 2023
@roger2hk
Copy link

This is the expected behaviour in Go 1.20. Please take a look at the Go 1.20 minor library changes.

The [math/rand](https://tip.golang.org/pkg/math/rand/) package now automatically seeds the global random number generator (used by top-level functions like Float64 and Int) with a random value, and the top-level [Seed](https://tip.golang.org/pkg/math/rand/#Seed) function has been deprecated. Programs that need a reproducible sequence of random numbers should prefer to allocate their own random source, using rand.New(rand.NewSource(seed)).

Programs that need the earlier consistent global seeding behavior can set GODEBUG=randautoseed=0 in their environment.

The top-level [Read](https://tip.golang.org/pkg/math/rand/#Read) function has been deprecated. In almost all cases, [crypto/rand.Read](https://tip.golang.org/pkg/crypto/rand/#Read) is more appropriate.

If you add the rand.Seed(1), the result in Go 1.20 is exactly the same as Go 1.19.

func init() {
    rand.Seed(1)
}

https://go.dev/play/p/ZrxLwB7yS5A

@nussjustin
Copy link
Contributor Author

@roger2hk This is not about the change in the behaviour of math/rand but about the playground not caching the program execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

8 participants
@roger2hk @phenpessoa @nussjustin @gopherbot @seankhliao @mrwormhole @findleyr and others