Skip to content

Commit 9159a9f

Browse files
author
Lauren McCarthy
authored
Merge pull request #1456 from nthitz/randomArray
add random(Array) to get a random element from an array of choices
2 parents 5d64288 + dd5f3d9 commit 9159a9f

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/math/random.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
116125
p5.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;

test/unit/math/random.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ suite('Random', function() {
5656
assert.isTrue(result < 10);
5757
});
5858
});
59+
suite('random(["apple", "pear", "orange", "grape"])', function() {
60+
test('should return a fruit', function() {
61+
var fruits = ['apple', 'pear', 'orange', 'grape'];
62+
result = random(fruits);
63+
assert.include(fruits, result);
64+
});
65+
});
5966
});
6067

6168
});

0 commit comments

Comments
 (0)