# Split array into random groups

**URL:** <https://forum.kirupa.com/t/split-array-into-random-groups/260338>\
**Category:** flash\
**Created:** [May 14, 2008, 3:42pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338 "2008-05-14T15:42:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcmcgeary](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@dcmcgeary](https://forum.kirupa.com/u/dcmcgeary)\
**Post date:** [May 14, 2008, 3:42pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/1 "2008-05-14T15:42:02Z")

</div>

Howdy,

I have an array of names and I want to be able to generate a random number and then split this array into groups with relatively equal numbers of names per group (stored in new arrays/strings). Right now I am doing this by defining a large number of separate arrays and then rotating dropping names into the new arrays with a LONG list of if/else statements. Is there a faster way to do this?

Example:

array = name1, name2, name3,…,name30  
random number = 5

I want to be able to create 5 new arrays and then randomly extract values from the array using a random number generator and then sort them into the 5 new arrays evenly. I can use the if/else statements if I have to, but I am hoping that there is an elegant way of doing this that won’t require so much in the way of coding.

Thanks!  
David

---

<div class="post-metadata">

**Author:** ![Valaran](https://avatars.discourse-cdn.com/v4/letter/v/ee59a6/32.png) [@Valaran](https://forum.kirupa.com/u/Valaran)\
**Post date:** [May 14, 2008, 5:06pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/2 "2008-05-14T17:06:03Z")

</div>

Not completely sure how you want it, but I’ll show you the deal with 1 array, and you can duplicate it for more than 1 if this is what you need.

```auto

// Old array
var oArr:Array = [name1, name2, ....];
// New array
var nArr:Array = [];

function ranNumber(high:Number, low:Number):Number {
    return Math.floor(Math.random() * (high - low)) + low;
}

for(var i in oArr){
    // Random number depending on current length of the array
    var ran:Number = ranNumber(0, oArr.length - 1);
    // Push the new array with the value from the old array
    if(ran == 0){
        nArr.push(oArr[ran].shift());
    } else {
        var tmp = oArr[ran];
        oArr[ran] = oArr[0];
        oArr[0] = tmp;
        nArr.push(oArr[ran].shift());
    }
}

```

This hasn’t been tested or anything, so there might be faults in it.

Edit: Due to boredom, I made one for 5 arrays aswell:

```auto

// Old array
var oArr:Array = [
                        ["name1", "name2", "...."],
                        ["name1", "name2", "...."],
                        ["name1", "name2", "...."],
                        ["name1", "name2", "...."],
                        ["name1", "name2", "...."]
                      ];
// New array
var nArr:Array = [[], [], [], [], []];

function ranNumber(high:Number, low:Number):Number {
    return Math.floor(Math.random() * (high - low)) + low;
}

for(var i:Number = 0;i<oArr.length;i++){
    for(var j in oArr*){
        // Random number depending on current length of the array
        var ran:Number = ranNumber(0, oArr*.length - 1);
        // Push the new array with the value from the old array
        if(ran == 0){
            nArr*.push(oArr*[ran].shift());
        } else {
            var tmp = oArr*[ran];
            oArr*[ran] = oArr*[0];
            oArr*[0] = tmp;
            nArr*.push(oArr*[ran].shift());
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)\
**Post date:** [May 14, 2008, 5:38pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/3 "2008-05-14T17:38:17Z")

</div>

yeah that wont work  
shift just removes and returns the first element in an array

for(var i in oArr){ // Random number depending on current length of the array  
var ran:Number = ranNumber(0, oArr.length - 1); // Push the new array with the value from the old array nArr.push(oArr.splice(ran,1));  
}

but i doubt you really want to do it the way you described it.  
say you wanted your mainArray to be split into groups of five  
currIdx = 0  
variance = 5  
arrays = new Array()  
while(currIdx \< origArray.length){  
arrays.push(origArray.slice(currIdx,variance))  
}  
for(var i = 0;i\<arrays.length;i++){  
trace(arrays\*)  
}  
that way you get a series of arrays that have 5 numbers in each without destroying the original array so you can shuffle it or reference it later

---

<div class="post-metadata">

**Author:** ![dcmcgeary](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@dcmcgeary](https://forum.kirupa.com/u/dcmcgeary)\
**Post date:** [May 14, 2008, 6:22pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/4 "2008-05-14T18:22:33Z")

</div>

I can’t thank you two enough! I like both suggestions. Based on what you two have shown me, I think that I’ll just randomize the content of the original array and slice it. I’m sorting the contents of the original array, anyways.

On a side note, can I use text strings + var ie: (“myStr” + i) to assign values to a string based on the value of i? I do something like this with setProperty and mc’s, but I wanted to know if you can do this with a string.

For example:

for (i=0, i \< num, i++) {  
“myStr” + i = “some string”;  
}

trace(myStr1) gives "some string"  
trace(myStr2) gives "some string"  
etc.

I know it doesn’t work like this, but I was hoping that there is something I can do similar to it.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)\
**Post date:** [May 14, 2008, 7:46pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/5 "2008-05-14T19:46:40Z")

</div>

this[“myStr” + i] = “some string”;

trace(myStr1)

---

<div class="post-metadata">

**Author:** ![dcmcgeary](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@dcmcgeary](https://forum.kirupa.com/u/dcmcgeary)\
**Post date:** [May 14, 2008, 7:54pm UTC](https://forum.kirupa.com/t/split-array-into-random-groups/260338/6 "2008-05-14T19:54:44Z")

</div>

Brilliant!!!

Thanks!
