Randomness in Madeup
About 30% into writing tests for a piece of code, my energy deflates. When I consider how many more tests I have to write to thoroughly test my code, I am overwhelmed. The worst part about testing is that you can’t just tax the moving parts of your software the way someone like you would. You have to step into the mind of your most incompetent user. I’m guessing criminal investigators face a similar problem when hunting down a homicidal maniac. Both of us have to twist our brain into thinking irrational and perverse thoughts to crack the case.
I then decide that the best thing for me to do is randomly generate a bunch of input data and see how my software handles it. That’s what I can do now in Madeup: generate random data. Two new constructs exist: a special seed
variable and a random
function:
seed = 10 -- chooses random sequence 10 n = random lo hi -- get a random number from [lo, hi]
If the seed is not set by the user, the current clock value is used.
I offer a few examples of random’s use.
Random Dots
nsides = 30 repeat 150 radius = random 1 3 x = random -10 10 y = random -10 10 z = random -10 10 moveto x y z end dots
Random Top
moveto 0 0 0 seed = 10 nsides = 40 repeat 10 radius = random 1 5 move 1 end tube
Random Strand
moveto 0 0 0 nsides = 10 radius = 4 seed = 27 repeat 300 dimension = random 0 2 angle = random 50 70 if dimension == 0 yaw angle else if dimension == 1 pitch angle else roll angle end end move (random 1 3) end dots
Random Terrain
size = 50 seed = 60 for r to size for c to size moveto c r 0.5 * (random -1 1) end end surface size size