root/trunk/as2/com/jeroenwijering/utils/Randomizer.as @ 1

Revision 1, 0.8 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

  • Property svn:executable set to *
Line 
1/**
2* Pick random array indexes without having the same picked twice times.
3*
4* @author       Jeroen Wijering
5* @version      1.2
6**/
7
8
9class com.jeroenwijering.utils.Randomizer {
10
11
12        /** a reference of the original array **/
13        private var originalArray:Array;
14        /** a copy of the original array **/
15        private var bufferArray:Array;
16
17
18        /**
19        * Constructor.
20        *
21        * @param arr    Array to randomize.
22        **/
23        public function Randomizer(arr:Array) {
24                originalArray = arr;
25                bufferArray = new Array();
26        };
27
28
29        /** Randomly pick an index from the array given. **/
30        public function pick():Number {
31                if(bufferArray.length == 0) {
32                        for(var k=0; k<originalArray.length; k++) {
33                                bufferArray.push(k);
34                        }
35                }
36                var ran = random(bufferArray.length);
37                var idx = bufferArray[ran];
38                bufferArray.splice(ran,1);
39                return idx;
40        };
41
42
43}
Note: See TracBrowser for help on using the browser.