I made this using ClojureScript, same as I did back when I made #Wastrle. But, with Wastrle, I used the Reagent library, https://reagent-project.github.io/, which is a ClojureScript wrapper around React that I know believe is absolute sorcery.
I really enjoyed the way Reagent let me think about the design of Wastrle. However, I wanted to challenge myself with making something similar, though less intricate, using just ClojureScript.
The computer rolls a die and tells you the result. You're supposed the guess the type of die rolled. If you do, you will. If not, the computer rolls again and you get another shot.
At the moment, the possible dice include d4, d6, d8, d10, d12, d20, d30, d00, and dF.
I'm open to implementing more dice, but right now, I wanted to stick to dice I've had in my collection for at least 30 years. (less that really weird d24)
There were a lot of really fun things to implement in this one.
One challenge, was to make a pseudo-random number generator.
Obviously, I wanted random numbers, but not just any random numbers! I wanted to be able to ask your browser what day it was, feed that date into my random number generator, and get the same sequence of "random" numbers that everyone else was getting on that date.
There's no random number generator on a HP-12c, so if I wanted to a pocket dice-roller, I had to code up one of these formulas.
The basic idea is that you feed it a number and it spits out a number that you wouldn't expect, in a particular range, where it is somewhat uniformly distributed.
Then you divide your total by M and use the remainder. (It's a little more nuanced than that, but for our purposes, this works.)
The result is a number between 0 and M. If you choose a good A and C, this number will feel random.
If you divide this result by M, you get a number between 0 and 1 (not including the 1), which you can multiply by the die size and add 1 to get a "random" roll.
It's a neat trick that works well, if you pick decent enough values for A, C, and M. Fortunately, plenty of diligent people have already figured out decent enough values for you. Check out the Wikipedia article above for more details.
For my purposes, this is great. I just turn the current date into the initial seed number and off we go!
Nice. I feel like I remember learning that my x86 computers did this each time I turned it on to seed any need for "random" numbers during that period.
I got to solve this with recursion, which is always exciting to me.
The end result is a bit of a mess because eventually I realized that I never want it spitting out nine d6s or whatever. It would just be too much of a hassle making the "graphics" of the site adapt to that. So when the numbers get big, you start rolling a suspicious amount of 6s.
But it was fun thinking through this problem recursively!
@epidiah A calculator manual was the first time I found the world “pseudorandom”, and I remember being confused that I couldn't find the definition in our big dictionary. It was for a Blackjack game.
@tresi I spent so much time trying to figure out the significance of the number 997 because, perhaps by tradition, that was the number all the calculator programs I saw used for their multiplier. I was like, "Is there something magical about this number?"
I think it's just the highest 3-digit prime and that felt right to people.