# Batch generation of straight line questions with images and answers saved to disc

I'm looking for a Mathematica solution to generate (a lot of) student questions for interpretation of a straight line graph, i.e. an image of a straight line where the student should find the expression y=kx+m given the image.

Is it possible to have Mathematica to loop over say n=100 iterations and for each cycle:

1. Generate two random numbers, k and m, (let's make it integer on [-10,10] for simplicity)
2. Define a function y=kx+m
3. Plot y (with fixed, uniform, dimensions so all graphs have the same 'look')
4. Save image to disk
5. Append the answer (y=kx+m, e.g. 'y=-3x+5') to a list

and at the end save the 'answer list' to disc?

TIA

Follow up (#2). After some testing

Clear[y, x, k, l]
For[
i = 1, i < 3, i++,
k = RandomInteger[{-5, 5}];
l = RandomInteger[{-5, 5}];
y[x_] := k x + l;
Print [y[x]];
H = Plot[y[x], {x, -3, 3}];
ImageFileName =
Export[ImageFileName, H, "PDF"];
]


Is the syntax good?

• Thanks, I tried to start easy and I do get y(x) printed on screen but the Plot function does not seem to display. (I would like to add my code there, but I don't know how to. Found out how to add code to original post, sorry.)
– mf67
Jul 26, 2018 at 10:15
• Take a look at Table and Map instead of trying a For loop. Table will return a list of results that you can save into a variable for later export, printing etc. Jul 26, 2018 at 13:02

This should give you a fairly good template:

Define a function that takes an ID and returns a suitably randomized graph of a line that includes some clearly labelled points.

lineProblemGeneratorPointsGiven[problemIDNum_] := Block[
{m, k, visibleDomain, shownXVals, shownPoints},
m = RandomChoice[Join[Range[-5, -1], Range[1, 5]]];
k = RandomInteger[{-5, 5}];
lineEq[x_] := m x + k;
visibleDomain = Clip[Sort[{(-20 - k)/m, (20 - k)/m}], {-20, 20}];
shownXVals = RandomSample[Range @@ IntegerPart[visibleDomain], 2];
shownPoints = ({#, lineEq[#]} & /@ shownXVals);
graphicForStudent = Plot[
lineEq[x],
{x, -20, 20},
PlotRange -> {{-20, 20}, {-20, 20}},
Frame -> True,
FrameStyle -> Bold,
GridLines -> Transpose[shownPoints],
Epilog -> {
PointSize[0.015], Red, Point[shownPoints],
Black, Text[Style[ToString[problemIDNum], 30], {-19.5, 17}, {-1, 0}]
},
FrameTicks -> {{Transpose[shownPoints][], None}, {Transpose[shownPoints][], None}}
];
{Rasterize[graphicForStudent, RasterSize -> 500], problemIDNum, m, k}
]


Now use it to generate a batch of problems:

my5Problems = lineProblemGeneratorPointsGiven /@ Range;


Now you want to export this so you have a folder of images and a spreadsheet of answers, so go ahead and make some directory lineProblemStorage. Include inside that one some directory imageStorage.

 linePicsWithID = #[[1 ;; 2]] & /@ my5Problems;
Export[FileNameJoin[{lineProblemStorage, imageStorage, ToString[#[]] <> ".jpg"}], #[]] & /@ linePicsWithID


Now imageStorage contains the images to be given to the students. You need your answer sheet, though.

myAnswers = Join[{{"ID", "m", "k"}}, #[[2 ;; 4]] & /@ my5Problems];


I seem to remember tests being averse to giving students labelled points when reading from graphs in order to prepare them for the real world where graphs are only used to communicate numerical minutiae of the function in question rather than to eyeball a solution for sanity or present themes and trends./s

If you want to be cruel and have your students strain their eyes and second guess themselves finding points that fall on the grid, you can use this generating function instead:

lineProblemGeneratorGridLines[problemIDNum_] := Block[
{m, k, visibleDomain, shownXVals, shownPoints},
m = RandomChoice[Join[Range[-5, -1], Range[1, 5]]];
k = RandomInteger[{-5, 5}];
lineEq[x_] := m x + k;
graphicForStudent = Plot[
lineEq[x],
{x, -20, 20},
PlotRange -> {{-20, 20}, {-20, 20}},
Frame -> True,
FrameStyle -> Bold,
GridLines -> {Range[-19, 19], Range[-19, 19]} ,
Epilog -> {Black, Text[Style[ToString[problemIDNum], 30], {-19.5, 17}, {-1, 0}]}
];
{Rasterize[graphicForStudent, RasterSize -> 500], problemIDNum, m, k}
]