forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrandomRangeSet.as
More file actions
32 lines (29 loc) · 808 Bytes
/
randomRangeSet.as
File metadata and controls
32 lines (29 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package utils.range {
import utils.number.randomIntegerWithinRange;
/**
* Returns a set of random numbers inside a specific range (unique numbers is optional)
*/
public function randomRangeSet(min:Number, max:Number, count:Number, unique:Boolean):Array {
var rnds:Array = new Array();
if(unique && count <= max - min + 1) {
//unique - create num range array
var nums:Array = new Array();
for(var i:Number = min; i <= max; i++) {
nums.push(i);
}
for(var j:Number = 1; j <= count; j++) {
// random number
var rn:Number = Math.floor(Math.random() * nums.length);
rnds.push(nums[rn]);
nums.splice(rn, 1);
}
}
else {
//non unique
for(var k:Number = 1; k <= count; k++) {
rnds.push(randomIntegerWithinRange(min, max));
}
}
return rnds;
}
}