P.Mean: Randomly generating simple math
problems using R (created 2009-11-30). |

To help drill simple concepts in math for my second grade son, I developed a series of R programs to generate these problems randomly. It makes use of the sample function on a sequence of integers and allows you to limit or expand the scope of the problems generated. It is far from perfect, but it shows a few simple tricks in R.

`###########################################`

# 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[1] <- max(x)
# Place the larger number at the top.

y[2] <- min(x)
# Place the smaller number in the middle.

y[3] <- y[1]-y[2]
# 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[2],"-",adj=1,cex=2) # Display minus sign

segments(30,35,50,35)
# Draw line beneath middle number.

}

The steps are pretty simple. Set up zero exterior margins for the graph and display 30 graphs in a 6 by 5 matrix. For each of the 30 graphs , randomly select two numbers between 1 and 10. Place the larger number at the top, the smaller number in the middle, and calculate what the difference would be. If you didn't force the larger number to the top, you might end up with some negative values which are too messy and complex for a second grader.

In each graph, randomly select one of the three positions (1=top, 2=middle, and 3=bottom) to be shown as unknown (a square box). Display the numbers for the remaining two positions.

Here are the results of thirty randomly selected subtraction problems, shown as a PDF file.

You can also generate two digit additions that do not require "carrying." Just insure that the "ones" column does not add up to more than nine and also insure that the tens column does not add up to more than nine.

`###################################`

# 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[1] <- (x.tens+x.ones)[1] #
Place smaller number at top

y[3] <- (x.tens+x.ones)[2] #
Place larger number at bottom

y[2] <- y[3]-y[1]
# 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

}

Here are the results of thirty randomly selected addition problems, also shown as a PDF file.

This work is licensed under a Creative Commons Attribution 3.0 United States License. This page was written by Steve Simon and was last modified on 2010-04-12. Need more information? I have a page with general help resources. You can also browse for pages similar to this one at Category: R software.