Home » Math Topics » Fractions Decimals and Percents » Pick a Number Between 0 and 1 — with Fractions!

# Pick a Number Between 0 and 1 — with Fractions!

This applet is a pick a number game, where the secret number is a fraction between 0 and 1. Enter a numerator and denominator and the computer will tell you if your fraction is too high or too low, until you guess the Secret Fraction and win.

Your previous high and low guesses are listed on the page (unless you hide the list), and there is a number line that graphs the guesses. You can change the number of divisions in the number line; the labels update automatically.

The game has several levels. Level 1 uses only fractions with denominators 2, 4, and 8, whereas Level 6 uses denominators from 2 to 16.

The applet doesn’t fit well on the page with my website theme, so you can open a new window and try it here. (Note: if you don’t see a fraction in the pink box, go to the bottom of the page and choose, “View as Java Applet,” which is what I wrote this applet as.  It does not seem to have converted correctly to HTML 5).

This applet was the most challenging to write of all the GeoGebra applets I’ve written to date — I wasn’t sure it was possible to create this game using GeoGebra, so it was quite satisfying when it came together. Here were some of the tricky parts (you can download my original file here):

## Picking the Secret Fraction

Deciding how to pick a secret fraction was complicated and more interesting than expected; this is a mathematical issue, not a GeoGebra issue.

I wanted the fractions to be simplified and between 0 and 1. There are, of course, an infinite number of simplified fractions between 0 and 1, so some I needed some parameters around the possible fractions. I liked the idea of having different levels of the game, and it made sense to limit the possible denominators in each level.

Once the possible denominators were set, I had to decide how to randomize. One solution that is clunky to program is to make each possible simplified fraction equally likely to be picked. Thus at Level 1, with possible denominators 2, 4, and 8, we would simply choose from the set {1/2, 1/4, 3/4, 1/8, 3/8, 5/8, 7/8}. The advantage of this uniform method is that at any stage of the game, every fraction in the remaining range is equally likely to be the secret number.

The disadvantage of the uniform method is that at the higher levels of the game, fractions with higher prime denominators, i.e. the kinds of fractions that people see the least often in mathematics and in everyday life, are much more likely to be chosen as the secret number. For example, at Level 5, where denominators can be between 2 and 12, there are 45 different simplified fractions, and 16 of them – a little over a third – have denominator 7 or 11.

I chose instead to use a “denominator first” method: I first randomly choose a denominator from the available denominators and then randomly choose a numerator from the natural numbers less than the denominator and relatively prime to it. Thus, if the denominator choice is 11, the numerator choices are 1 to 10, and if the denominator choice is 12, the numerator choices are {1, 5, 7, 11}. Thus, 7/12 is 2.5 times as likely to be the secret fraction as 7/11, but it’s more likely that the secret fraction will be one of 6/11, 7/11, or 8/11 than that it will be 7/12. I can live with this.

This unequal distribution of potential secret fractions affects probabilities as the game is played. For example, say we are playing at Level 6, where denominators can range from 2 to 16, and we know that the secret fraction is less than 1/8. The remaining possibilities are 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, and 1/16. It is 3 times as likely that the fraction is 1/12 as that it is 1/13. Again, I can live with this situation, where the fractions that students are more likely to guess are also more likely to be the secret fraction.

It is true that at the lower levels, 1/2 is the secret fraction too often, and the uniform selection method might make more sense when the denominators are all more familiar.

Another method I considered is (electronically) rolling two dice, repeating until the rolls are unequal, and then simplifying the smaller number over the larger. This is a practical method for randomly picking a secret fraction when students are playing the game without a computer and when the denominators are chosen from a consecutive range. However, it shares negative features with the other two methods.

When no fractions with a given denominator can be simplified — either because the denominator is prime or because it is greater than half the maximum denominator, then the dice roll method works similarly to the uniform method.   For example, in Level 5, to get a denominator 11, one die has to show 11, and the other some number between 1 and 10. To get a denominator of 12, one die has to show 12, but the other die can only show 1, 5, 7, or 11; otherwise the fraction will be simplified to a lower denominator.

The dice roll method differs from the uniform method in that like the denominator first method it makes very small denominators more likely than larger denominators. At Level 5 there are 6 different unordered pairs that simplify to 1/2. The combination of a distribution that is often uniform, but with small denominators highly over-represented, makes some common fractions very unlikely to be chosen as the Secret Fraction.  At Level 6, with denominators up to 16, the dice roll method gives only a 1/30 chance of the Secret Fraction having denominator 10 and also a 1/30 chance of denominator 12, whereas there is a 1/10 chance of denominator 13, a 1/10 chance of denominator 7, and a 1/12 chance of denominator 3 .

When I started making the applet, I hadn’t expected choosing the secret fraction to be so complicated (and so interesting).

## Entering the Guess

Initially I used text boxes for entering guesses. I first tried using one text box, where players would enter a fraction such as “2/3.”  GeoGebra would then automatically change the fraction to a decimal in the box, and I couldn’t figure out a good way to hide the decimal. GeoGebra was also fine with players entering decimals, and I wanted the game to be played with fractions.

Then I tried using two input boxes: one each for the numerator and denominator. The problem was how to update the guesses. They couldn’t be updated every time the player pressed enter, because then there would be intermediate guesses; e.g. if after guessing 2/3, a player wanted to guess 4/5, and she entered the numerator 4, then GeoGebra would record 4/3 as another guess, before recording 4/5.

I tried using a button to update the numerator and denominator guesses together, as in the final version, but then the player had to press enter for each text box before pressing the button, and it was unnatural to remember to do this. Otherwise, the second number entered wouldn’t update, so clicking on the button would first cause an unintended guess to be checked against the secret number, and then the click itself would cause the variable to update.   In the above example, the computer would check 4/3 against the Secret Fraction, and then it would update the value on the screen to 4/5.

Syncing when entering multiple variables before checking something has been an issue that others have had with GeoGebra in different contexts, and there does not appear to be a good solution. The text boxes are tied to specific variables, and there needs to be a click or enter event to update them.

I decided on sliders to enter the variables. A nice thing about using the sliders is that I could set them up to only enter allowable fractions for a particular level. The denominator slider is a dummy variable that references a list of possible denominators. The numerator slider goes from 1 to the value of the denominator slider, so that only fractions less than or equal to 1 can be entered.

## Recording Guesses

I spent a fair amount of time trying to use the spreadsheet to record previous guesses. I had two issues that were hard to resolve and caused me to abandon the approach. The first was that I couldn’t figure out how to reset the spreadsheet so that new games would start at the top of a column. The second was that the Latex didn’t always work correctly to display fractions in the spreadsheet.

I ended up just keeping lists of high and low guesses in text form, and displaying them on the screen. It was fun to figure out formulas for the correct positions. A new column starts after 6 guesses; one tricky part was that the Mod[x,6] function cycles 1, 2, 3, 4, 5, 0, and I wanted 1, 2, 3, 4, 5, 6, so I ended up using an If function to change the last 0 to a 6.

## Number Line

When I first thought about doing this applet in GeoGebra, I thought the number line would be a lot harder to program than it actually was. I used the existing coordinates within GeoGebra for graphing the points, but I made my own tick marks and labels, using the Sequence command to create lists that changed based on the value of the number of divisions. With the exception of a little tweaking, this part worked correctly the first time. Very satisfying!

I thought that I would need to incorporate zooming to see enough detail on the number line, but everything looked fine on my laptop as is (on a phone screen, that might be a problem).

One thing I wasn’t counting on was how easy the number line makes the game. A player can just move the divisions slider until he finds a fraction between the lowest high guess and the highest low guess. (To make the game more difficult, hide the number line).

Nonetheless, I think the number line representation is very good for helping develop fraction number sense. I am thinking about ways to use such a number line in other applets.

## Scripting

I initially got the idea of a fraction pick a number game a few years ago when I decided to update my programming skills and learn a newer language. In the introductory Python book I was using, there was a whole number pick a number game as a programming exercise. I wrote it instead for fractions, but with very clunky prompts to enter numerator and denominator (this part I wrote more-or-less as if I were using Pascal in the 1980’s). I got stuck on how to do the graphic and event driven parts, got busy, and moved onto other things.

Writing this applet in GeoGebra has helped me understand some of these other aspects of programming. All of the “guts” of the program happen in scripts; most of the logic is in the script for the button to enter guesses. It wasn’t too hard to figure out where bits of programming should go – the “You Win” script shows up when the guess as a decimal is equal to the Secret Fraction as a decimal, the number of tries is set to 0 when the “New Game” button is clicked, etc.

Now I am contemplating how I might incorporate writing some simple GeoGebra scripts into my math classes. Very few of my “digital native” students have any programming experience, but they tend to take to GeoGebra fairly quickly. I’d like them to feel some of the same kind of satisfaction that I felt when I finally got this applet to work.