# Random Note Color

Is there a way to have the color of a note select a color at random, or better, a color at random from an array of preselected colors? Just curious. Sure.

Make a list of colors you want

. \$MyList=“red; green;blue;poppy;black;white”

The nth color on this list is \$MyList.at(n), where n={0,1,2…5}.

The expression

rand()

generates a random number from 0 to 1. So

\$MyNumber=rand(); \$MyNumber=floor(6*\$MyNumber)

will give us the random number we want. (There’s an issue with complex expressions containing rand(), which will be fixed shortly; doing this in two steps is a workaround.

Should this be floor(6*\$MyList)?

No. Normally, I’d write

\$MyNumber=floor( 6*rand() )

But there’s an issue in 8.2.1: rand() is confused about what sort of thing it returns, and multiplication needs to know what sort of thing is multiplying. So, we do it in two steps:

\$MyNumber=rand();
\$MyNumber=floor(6*\$MyNumber);

Thanks for the clarification. I was confused about where the code references \$MyList, but now I’m guessing that \$MyNumber and \$MyList already know about each other under the hood.

I think the pieces go together like so:

``````\$MyList="red;green;blue;poppy;black;white";
\$MyNumber=rand();
\$MyNumber=floor(6*\$MyNumber);
\$Color=\$MyList.at(\$MyNumber);
``````
3 Likes

Indeed they do! Thanks, @mwra!