xMath API

A free API for generating random mathematical expressions.

View on GitHub


I've always liked math, so one of my open source projects was an oral counting simulator. To do this, I wrote code that generates random expressions right inside the application. Now, I decided to separate the application logic and make a free API out of it so that you can create your cool apps.

How to use it?

The API provides you with the ability to get a random expression with mathematical operations such as addition, subtraction, multiplication or division. You get an object containing two numbers, an operation sign, and the result of an expression.

You can choose the operation you need or get a random operation.

Random expression

Only addition

Only subtraction

Only multiplication

Only division


By default, the range of numbers from 1 to 99 is set for all expressions. But you can adjust the range (for any operation) yourself using the parameters.

Range of generated numbers

The max and min parameters change the possible range for each number in the expression.

The maxFirst and minFirst parameters change the possible range only for the first number (the second number uses the default value).

The maxSecond and minSecond parameters change the possible range only for the second number (does not affect division, because there the second number is randomly selected from the list of divisors of the first number).

Negative result

To get expressions that result in negative numbers, you can change the range of values using the parameters described above by adding negative numbers there.

But this will not work for the subtraction operation, because by default there the first number is always greater than the second. To fix this, you can use the parameter negative in the value 1.