#ThisWeeksFiddler, 20260619

This week the #puzzle is: Can You Fix the Random Number Generator? #probabilities #montecarlo #coding #expectedvalue

I think the random number generator on my calculator might be malfunctioning. Oh no!
Under normal conditions, it should generate random numbers between 0 and 1. But my suspicion is that the calculator is “tanked,” meaning it only generates random numbers between 0 and some value 0 < a < 1. Beyond that, I have no knowledge regarding the value of a. At the moment, it’s equally likely to be any value from 0 to 1.
As an experiment, I ask the calculator to generate one random number. It produces a value of exactly 0.5. (While this is, admittedly, infinitely unlikely, let’s roll with it!)
Based on this result, what can I expect the value of a to be, on average?

And for extra credit:

Frustrated with my old calculator, I toss it in the trash and buy a new one. But now I’m concerned this second calculator is also “tanked.” As before, every value of a between 0 and 1 is equally likely at first.
I ask my friend to generate one random number using this second calculator. My friend does so, and smirks. “I won’t tell you what the number is,” my friend says, “but it’s somewhere between 0 and 0.5.”
On average, what can I expect the value of a (for this second calculator) to be?

Can You Fix the Random Number Generator?

Solution, possibly incorrect:

Program

Method 1: Something, something, sums, integrals. PDFs? I got stuck.

Method 2: Monte Carlo. Choose a random a, then produce a random output for the calculator. (If r is a random number 0-1, then ra is a random number 0-a. Neat.) If that random output is reasonably close to 0.5 (within a difference of Δ\Delta), use that a to calculate the average a.

actual loops   good loops        delta    average a
71468147 10000000 0.100000 0.714385
360434645 10000000 0.020000 0.721046
1803734570 10000000 0.004000 0.721364
9014346353 10000000 0.000800 0.721287
45085162688 10000000 0.000160 0.721386

Result: 0.721. We know a is somewhere between 0.5 and 1, so that makes sense.

And for extra credit:

Still monte carlo. Choose a random a, then produce a random output for the calculator. If that random number is 0.5 or below, use that a.

actual loops   good loops                 average a
11812250 10000000 0.442937

Result: 0.443. The lower the a, the higher the chance of producing an output below 0.5. Makes sense.

This was actually much easier as monte carlo!

Forfatterens død

Anmeldelse af Death of the Author, af #NnediOkorafor. Roman. 2025. Hugo-finalist. Nebula-nomineret.

Nebula nominees . Hugo finalists .

Skitse: Zelu er forfatter og underviser, men får endnu et afslag på en tekst i det ene område og bliver fyret fra det andet. I frustration begynder hun på en ny bog, der bare flyder ud af hende. Den her gang skal det handle om robotter i fremtiden.

Er det science fiction? 95-100 % science fiction, ja. Noget af det er robotterne, så det er fint. Noget af det er Zelu, der bruger selvkørende taxaer og via teknologi kommer til at gå igen, efter et uheld, da hun var 12, så det er nær fremtid. Endelig er der interviews med Zelus familie og venner, der foregår et stykke inde i fremtiden også.

Temaer: Zelus familie fylder meget. Zelu går ikke efter altid at tilpasse sig, og hendes familie reagerer som regel, som om hun kaster skam over dem alle. Da Zelu laver så stort et plask, at hele verden opdager det, så rammer det hendes mor hårdt. Hendes hjerne “bliver ødelagt”. Det viser sig, at der var tale om et panik-anfald, og at moderen har haft dem før. (Suk.) På den anden side er familien fra Nigeria, så vi hører bl.a. om mad fra området. (Og jeg kaster mig ud i at lave egg stew. Mums.)

Zelus roman foregår i Nigeria. Det er derfor et hårdt slag, da filmatiseringen helt har slettet den del af historien. Det er bare én af de ting, en forfatter med succes oplever. Filmatisering? Jeps. Romanen var nærmest en bestseller, før et forlag havde sagt ja til den. Woohoo! Bagsiden af det er fans, der utålmodigt kræver bind 2 og på anden vis stiller store krav. Såsom: Når Zelu via teknologi lærer at gå igen, så er det en kommentar til andre handicappede, en opfordring til, at de skal ændre sig også. Eller sådan er der i hvert fald nogen, der ser det.

Zelu kæmper med diverse psykiske problemer. Det kræver løbende terapi, og på et tidspunkt er det godt for hende, at en ven af en ven nægter at have ondt af hende. Da hun finder en kæreste, så er det også noget af en omstilling. Hun er meget bevidst om det, første gang hun gør noget, primært fordi kæresten gerne vil have det.

Er det godt? Den sidste halvdel af bogen fungerede for mig. Men første halvdel var for hoppende. Hver gang, jeg var ved at komme godt ind i det, så var det næste kapitel i en anden tråd. Derfor ikke top-karakter. Desuden er der en overraskelse hen mod slutningen, som jeg ikke rigtig ved, hvad jeg skal tænke om. ##-

GridOS 1 quest 4, less steps

For part 1 I stumbled upon a solution by Favo02, that runs very fast. I read this solution and then recreated it on my own. Basically, instead of hammering the nails, hammer the log. Brilliant.

For part 2 and 3, it was back to generated code. In both cases, it took days to get the program to run. Cases 1-72, with 9 nails or less, were easy to write. But cases 73-100… Instead of just traveling right, the heads have to jiggle a little to cover both top and bottom of each column. I grew to really hate case 73.

I’m sure both of those programs could be optimized. Cleanup in part 3 is a bit messy.

Unlike quest 2, the programs didn’t have to list every possible combination in every single case. The combinations just couldn’t overlap. So e.g., looking for the 1st head of a nail, I could look for |!! and =|! and ==! in the case of 3 nails. (! is a wild card.) This made the programs shorter.

READ0		===_______	READ0	**********	RRRRRRRRRR
READ0 |!!_______ READ1 ********** RRRRRRRRRR
READ0 =|!_______ READ1 ********** RRRRRRRRRR
READ0 ==|_______ READ1 ********** RRRRRRRRRR

For some reason, I found it very hard to jiggle the heads. Up, over to mark the next column, then down. I spent such a long time fixing mistakes, and occasionally running into programs taking 10.000 steps in a single direction or running out of steps altogether.

All my code .

GridOS 1 quest 4, basic

In quest 4, nails will be hammered on the head.

I examine each nail, and when I encounter the head, I hammer it. This is also a 1 head program. For part 2 I have to find the shortest nails and hammer the rest down to that length. I use 3 heads and some meta data along the edges.

I examine each line from left to right to find the length. Then I examine all the lines from top to bottom. This way I can change state to “1st head found” and “now I just have to delete the rest” easily. This program could easily be optimized. Instead of writing my own @ at the beginning of each line, I could use the #’s already there. And I note which lines doesn’t have to change, so I could actually skip them quickly.

For part 3, the 2 shortest nails are unchanged and the rest are hammered down to the length of the almost shortest.

All my code .

GridOS 1 quest 3, less steps

For part 1, I reduced the number of steps by coming from both ends at once.

For part 2 I used 7 heads instead of 4. This allowed me to build layer 2 of extra pond by looking at the top of the pond directly. (~~~~~ in the top layer means digging in layer 2.)

For part 3 I used a version of the basis program.

Instead of going the full length of each line, I did each pond separately. This saved a lot of steps.

All my code .

GridOS 1 quest 3, 1 head

1 head, extra leg work.

I do a small square, when I detect, that there’s digging to do. In part 2 I add a character to mark the end of line 2, and I go right first, then left.

Part 3 is done in a different way. I wasn’t quite sure how to do this, and then I saw someone mention recursion. Oh yes! It requires going right initially, and when I go down always checking to the left.

All my code .

GridOS 1 quest 3, basic

This time we’re digging one or more ponds deeper. It took a long time for me to think of this solution.

In part 1 it’s 1 layer deeper, in part 2 2 layers, and in part 3 as many layers as there’s room for.

Part 3 might get a little crazy…

I’m using a mechanism, where I can look at 3 characters and write in a 4th position. In part 3 I have a 5th head, keeping track of whether I need to dig deeper. I also write extra characters, to make sure I stop at the end of each line.

All my code .

#ThisWeeksFiddler, 20260612

This week the #puzzle is: Can You Catch the Longest Wave? #geometry #trigonometry #coding #approximation #maximum #average

Semicircle Island is shaped like a perfect semicircle (or semidisk, technically), with a radius of 1 mile. It doesn’t have any permanent residents, but it’s a very popular destination for surfers.
Rumor has it that a big wave is headed toward the island, but no one knows which direction it’s coming from. This thin, straight wall of water never changes speed or direction. It will first make contact with the island at 10 a.m. and it will last be in contact with the island at 10:10 a.m.
What is the longest possible stretch of land that is directly under the wave at 10:05 a.m.?

And for extra credit:

Another wave is approaching the island, but again no one knows which direction it’s coming from—for the moment, all directions are equally likely. For example, it might come in the direction illustrated below:
On average, what is the length of the stretch of land directly under the wave halfway between when the wave first and last makes contact with the island?

Can You Catch the Longest Wave?

Solution, possibly incorrect:

Program Desmos Video

Method 1: Recreate the situation in Desmos and play around to find a maximum value. (The Desmos document includes an animation. So does the video 😉 )

Method 2: Write a program to systematically go through a lot of options and find a maximum value.

The methods share a lot of the same math.

Assuming the island is a circle, but with 0 <= x, it makes sense to only look at some of the possible angles for the wave.

0θπ20\le \theta \le \frac{\pi}{2}

Here the angle is the one between the x axis and a line perpendicular to the wave. The wave is a tangent to the circle, and the line crosses the wave at the tangent point. (This wave could be the incoming wave or the outgoing wave.) This point can now also be described as:

x1=cos(θ),y1=sin(θ)x_1=\cos(\theta), y_1=\sin(\theta)

So now we have a definition of the line representing the wave:

yy1=x1y1(xx1)y-y_{1}=-\frac{x_{1}}{y_{1}}\left(x-x_{1}\right)

The wave on the other side of the island goes through (0,-1) and has the same slope.

y(1)=x1y1(x0)y-\left(-1\right)=-\frac{x_{1}}{y_{1}}\left(x-0\right)

To learn something about the wave in the middle, the one we will try to restrict to the island, learn the length of and maximize, we begin with a single point on that line:

x2=x1+02,y2=y1+(1)2x_{2}=\frac{x_{1}+0}{2},y_{2}=\frac{y_{1}+\left(-1\right)}{2}

And now we can describe the line segment:

yy2=x1y1(xx2){0x1y2}y-y_{2}=-\frac{x_{1}}{y_{1}}\left(x-x_{2}\right)\left\{0\le x\le\sqrt{1-y^{2}}\right\}

To learn more, we need exact descriptions for the end points of the segment. The top point will either be on the y axis or on the circle.

x3a=0x_{3a}=0

x1y1(xx2)+y2=1x2-\frac{x_{1}}{y_{1}}\left(x-x_{2}\right)+y_{2}=\sqrt{1-x^{2}}

Changing this to the standard form:

ax2+bx+c=0ax²+bx+c=0

a=x12y12+1a=\frac{x_{1}^{2}}{y_{1}^{2}}+1

b=2x12x2y122x1y2y1b=-\frac{2x_{1}^{2}x_{2}}{y_{1}^{2}}-\frac{2x_{1}y_{2}}{y_{1}}

c=x12x22y12+y22+2x1x2y2y11c=\frac{x_{1}^{2}x_{2}^{2}}{y_{1}^{2}}+y_{2}^{2}+\frac{2x_{1}x_{2}y_{2}}{y_{1}}-1

x3b=bb24ac2ax_{3b}=\frac{-b-\sqrt{b^{2}-4ac}}{2a}

x3=max(x3a,x3b),y3=x1y1(x3x2)+y2x_{3}=\max\left(x_{3a},x_{3b}\right),y_{3}=-\frac{x_{1}}{y_{1}}\left(x_{3}-x_{2}\right)+y_{2}

And for the bottom point:

x4=b+b24ac2a,y4=x1y1(x4x2)+y2x_{4}=\frac{-b+\sqrt{b^{2}-4ac}}{2a},y_{4}=-\frac{x_{1}}{y_{1}}\left(x_{4}-x_{2}\right)+y_{2}

And now we can calculate the distance between the points:

d=(x4x3)2+(y4y3)2d=\sqrt{\left(x_{4}-x_{3}\right)^{2}+\left(y_{4}-y_{3}\right)^{2}}

Playing around with the original angle, it’s possible to find a max for d. This happens at:

θ=0.339837\theta=0.339837

dmax=1.885618d_{max}=1.885618

The program reaches the same conclusion. 1.885618.

And for extra credit:

Instead of looking for a max, convert the program to add all the distances together, to find an average.

Result: 1.304439.

I’m sure there’s a way to find a function, where d pops out, and then integrate over it for all angles. But, you know.