@@ -76,16 +76,19 @@ p5.prototype.randomSeed = function(seed) {
7676 * If no argument is given, returns a random number from 0
7777 * up to (but not including) 1.
7878 *
79- * If one argument is given, returns a random number from 0 up to
80- * (but not including) the number.
79+ * If one argument is given and it is a number, returns a random number from 0
80+ * up to (but not including) the number.
81+ *
82+ * If one argument is given and it is an array, returns a random element from
83+ * that array.
8184 *
8285 * If two arguments are given, returns a random number from the
8386 * first argument up to (but not including) the second argument.
8487 *
8588 * @method random
8689 * @param {Number } [min] the lower bound (inclusive)
8790 * @param {Number } [max] the upper bound (exclusive)
88- * @return {Number } the random number
91+ * @return {Number|mixed } the random number or a random element in choices
8992 * @example
9093 * <div>
9194 * <code>
@@ -106,13 +109,19 @@ p5.prototype.randomSeed = function(seed) {
106109 * </div>
107110 * <div>
108111 * <code>
109- * // Get a random element from an array
112+ * // Get a random element from an array using the random(Array) syntax
110113 * var words = [ "apple", "bear", "cat", "dog" ];
111- * var index = floor( random(words.length)) ; // Convert to integer
112- * text(words[index] ,10,50); // Displays one of the four words
114+ * var word = random(words) ; // select random word
115+ * text(word ,10,50); // draw the word
113116 * </code>
114117 * </div>
115118 */
119+ /**
120+ * @method random
121+ * @param {Array } choices the array to choose from
122+ * @return {mixed } the random element from the array
123+ * @example
124+ */
116125p5 . prototype . random = function ( min , max ) {
117126
118127 var rand ;
@@ -127,7 +136,11 @@ p5.prototype.random = function (min, max) {
127136 return rand ;
128137 } else
129138 if ( arguments . length === 1 ) {
130- return rand * min ;
139+ if ( arguments [ 0 ] instanceof Array ) {
140+ return arguments [ 0 ] [ Math . floor ( rand * arguments [ 0 ] . length ) ] ;
141+ } else {
142+ return rand * min ;
143+ }
131144 } else {
132145 if ( min > max ) {
133146 var tmp = min ;
0 commit comments