This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Pick a color uniformly at random in RGB color space

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.




RGB::random() returns a random color. The colors returned are distributed independently and uniformly in the RGB color space.


Example 1

RGB::random can be used to produce high-frequency noise:

plot(plot::Raster([[RGB::random() $ x = 0..42] $ y = 0..42]))

Example 2

It is possible to use RGB::random directly as a color function, but the result may be unexpected:

plot(plot::Waterman(5, FillColorFunction = RGB::random))

The reason is that the color function will be called repeatedly if the same point is met again. It is a better idea to create a bunch of random colors and then use the parameters passed into the color functions to get some consistency into the choice of colors:

colors := [RGB::random() $ i = 1..42]:
plot(plot::Waterman(5, FillColorFunction=((x,y,z,i) -> colors[i])))

Return Values

RGB color: A list of three floating-point values.

See Also

MuPAD Functions

Was this topic helpful?