Something I hadn't done before

I live for interesting problems, but most of professional software development is the same

Professional software development is a bit repetitive. I imagine it's like most professional fields. 95% of the time you're doing the same sort of things over and over again. But on occasion, you get to pull from your deep knowledge. Or hurdle someone backwards.

Like one time working on the LPGA, I got to do some recursion. But it turns out that wasn't a great idea, in that the data that I was recursing on, one time, was broken, and it ended in a stack overflow that crashed the site for a few minutes :P  (I'm quick to respond)

Sometimes, I get to use math! :D  I know, it's still useful sometimes.

I always like when there's a problem that I haven't solved before, let alone like 20 times or 200 times.

On rangram.com, similar to wordle and other daily puzzle games, the puzzle for the day is generated at midnight. This can be solved a number of ways.

Given: I have an array of all of the possible puzzles, like 62000 puzzles.

One way, for instance, is to check for an existing puzzle for that day, if you're storing all past puzzles (I am). If none exists for the current day, you generate a puzzle.

There are really so many ways to solve it. Another way, you take the date, hash the string, and see if that hash exists (could be problematic).

But then I wanted to modify the problem. Given the list of 62000 pre-defined puzzles, I don't want to be developing on local, like debugging an issue with something, and have the puzzle be different from what's on live. But I also want it randomized.

The obvious pretext to this solution is a seeded random number generator. I decided to use my birthday as the seed :)

seeddt := time.Date(1979, 3, 23, 13, 15, 0, 0, time.Local)
r := rand.New(rand.NewPCG(0, uint64(seeddt.UnixMicro())))

Go's math v2 rand package gives us this. So I have my seeded random number generator. The next step is given the current date, which puzzle should I get?

That is easy, and involves another hard coded date, the date that I launched the site.

loc, _ := time.LoadLocation("America/New_York")
a.startDate = time.Date(2025, 1, 22, 0, 0, 0, 0, loc)

And then, since I don't know a better way, I simply loop over the sequence of random numbers from 0 to [days since start date], grab the next one, and I have my random index.

I wanted to make sure I didn't pick one that was already picked, so I collect all of the generated random numbers in a map, and then when I want to get that days, I just loop through until I find one that it didn't pick yet.

So that's how I did it. I do also check that there's not a puzzle for that day, but in this case, I will always get the same puzzle on March 6 2025, or July 7 2030. It won't matter. Although it'll be looping over 1000 times by that point (cake), but I will probably look to see if there's a built in way to get the 1200th random number given a seed, without looping. It should almost be deterministic, I would think, knowing nothing about anything ;)

Happy coding!