This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Create a random polynomial

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


polylib::randpoly(<list>, <ring>, <Degree = n>, <Terms = k>, <Coeffs = f>, <Monic>)


polylib::randpoly() returns a univariate random polynomial with integer coefficients; the global identifier x is used as the indeterminate.

polylib::randpoly(list) returns a random polynomial in all indeterminates given in list.

polylib::randpoly(list, ring) returns a random polynomial in the indeterminates given in list over the coefficient ring ring.

See poly for a detailed description of possible indeterminates and coefficient rings.

The polynomial is created by randomly choosing as many exponents as specified through the option Terms and then choosing random coefficients. It may of course happen that for some coefficient 0 is chosen, therefore the actual number of terms in the result can be smaller than the value of the option Terms.

If the option Coeffs=f is given, the random coefficients are generated by calling f(). Otherwise, if ring is Expr, the coefficients will be random integers in the range - 999, …, 999. If ring is a user-defined domain, it must have a method "random" to create the coefficients if no function is given.

If the option Monic is given, the resulting polynomial has exactly the specified degree and the leading coefficient is 1.

If the requested number of terms exceeds the maximal possible number of terms for the specified degree and number of variables, a warning is emitted and a dense polynomial is created.

Environment Interactions

Unless a generator is specified through the option Coeffs, polylib::randpoly uses random to create the exponents and coefficients. Therefore it is sensitive to the environment variable SEED.


Example 1

We generate a univariate random polynomial in the indeterminate z, and use the default values for the other options. Therefore the polynomial has integer coefficients, is of degree 5, and has 6 terms.


Example 2

We create a bivariate random polynomial over the finite field with 7 elements. This works because Dom::IntegerMod has a "random" slot that generates random elements:




List of indeterminates


Coefficient ring



Option, specified as Degree = k

The maximum degree the result can have in each variable. k must be a nonnegative integer. Default is 6.


Option, specified as Terms = t

Makes polylib::randpoly generate the sum of t random terms. t must be a positive integer or infinity. If t equals infinity, polylib::randpoly returns a dense polynomial. Default is 5.


Option, specified as Coeffs = f

Create the coefficients of the result by calling f().


The created polynomial is monic, i.e., the leading coefficient is 1.

Return Values

Polynomial in the given indeterminates over the given ring. If no list of indeterminates is given, [x] is used. If no ring is given, Expr is used.

See Also

MuPAD Functions

Was this topic helpful?