#### Randomly generating simple math problems using R (created 2009-11-30)

```########################################### # Single digit subtractions with unknowns # ########################################### par(mar=rep(0,4))                  # Zero exterior margins for graph par(mfrow=c(6,5))                  # Display graphs in 6 by 5 grid. r <- c(0,100)                      # Set range for plot window v <- c(80,50,20)                   # Set vertical positions for (i in 1:30) {   y <- rep(NA,3)                   # Initialize to three missing values.   x <- sample(1:10,2)              # Select two numbers between 1 and 10.   y <- max(x)                   # Place the larger number at the top.   y <- min(x)                   # Place the smaller number in the middle.   y <- y-y                # Place the difference at the bottom.   plot(r,r,axes=FALSE,type="n")    # Draw an empty plot with no axes   j <- sample(1:3,1)               # Randomly pick unknown position   points(40,v[j],pch=0,cex=5)      # Display box at unknown value   for (i in (1:3)[-j]) {     text(50,v[i],y[i],adj=1,cex=2) # Display two known values   }   text(25,v,"-",adj=1,cex=2)    # Display minus sign   segments(30,35,50,35)            # Draw line beneath middle number. }```
```################################### # Two digit additions, no carries # ################################### par(mar=rep(0,4))                   # Zero exterior margins for graph par(mfrow=c(6,5))                   # Display graphs in 6 rows and 5 columns. r <- c(0,100)                       # Set range for plot window v <- c(80,50,20)                    # Set vertical positions for (i in 1:30) {   y <- rep(NA,3)                    # Initialize to three missing values.   x.tens <- sort(sample(1:9,2)*10)  # Select randomly from 10, 20, ..., 90   x.ones <- sort(sample(1:9,2))     # Select randomly from 1, 2, ..., 9   y <- (x.tens+x.ones)        # Place smaller number at top   y <- (x.tens+x.ones)        # Place larger number at bottom   y <- y-y                 # Place difference in the middle   plot(r,r,axes=FALSE,type="n")     # Draw an empty plot with no axes   for (i in 1:2) {     text(50,v[i],y[i], adj=1,cex=2) # Display top and middle values   }   text(25,50,"+",adj=1,cex=2)       # Display plus sign   segments(30,35,50,35)             # Draw line beneath middle number }```