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
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.
// 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:
// 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());
}
}
}
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
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!
this[“myStr” + i] = “some string”;
trace(myStr1)