0

How can I make an array in flash as2 and from there select 12 values assigning them to twelve different variables?

So far I got this:

quotes = new Array();


quotes[0] = "one";
quotes[1] = "two";
quotes[2] = "three";
quotes[3] = "four";
quotes[4] = "five";
quotes[5] = "six";
quotes[6] = "seven";
quotes[7] = "eight";
quotes[8] = "nine";
quotes[9] = "ten";
quotes[10] = "eleven";
quotes[11] = "twelve";
quotes[12] = "thirteen";
quotes[13] = "fourteen";
quotes[14] = "fifteen";
quotes[15] = "sixteen";
quotes[16] = "seventeen";
quotes[17] = "eighteen";
quotes[18] = "nineteen";
quotes[19] = "twenty";

Im keeping this structure because it will be easier to maintain in the long run and has more readability.

What I dont know is how to take twelve random values out of it and assign them to variables.

Ok, so I have now added this piece:

trace(quotes)
for(var i:Number = 0; i<12; i++){
          var x:Number = Math.floor((Math.random()*quotes.length));
          trace("X :: " + x);
          trace("ARRAY VALUE :: " + quotes[x]);
          quotes.splice(x,1);          
     }

Now I see in the trace 12 different values without repetition. But still I dont know how to make the results be the values of 12 different vars.

3
  • quotes[19] = "twenty";, maybe? Commented Dec 22, 2010 at 13:15
  • I'm a bit lazy to post a solution. Just google for "actionscript array shuffle" and you should find something to help you. Commented Dec 22, 2010 at 13:19
  • I removed the Flex tag because Flex does not support AS2. Commented Dec 22, 2010 at 13:56

2 Answers 2

1
var myArray = quotes.slice(); // make a copy so that the original is not altered //
n = 12;
for (var i:Number = 0; i < n; i++) {
    var randomSelection = Math.floor((Math.random() * myArray.length));
    trace("Selected: " + myArray[randomSelection]);
    myArray.splice(randomSelection, 1);
}

Shamelessly taken and adapted from a random forum.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Im going to try it. Just one thing there is a possibility to repeat the selection right? I need to make sure that none of the values is repeated.
Beware that this var myArray = quotes; doesn't make a copy, so both quotes and myArray point to the same object. You could make a shallow copy changing it to var myArray = quotes.slice(); though.
@Juan Pablo Califano Thanks for the info.
0

Math.random returns a number in the range [0-1), meaning it will never actually return 1, thus since you're flooring the value you're going to have to make the upper limit be n+1, where n is the true upper limit.

Now, it'd be good to know more about what the variables you want to use look like and whether or not they belong to the same object. I'm going to go ahead and assume that the variables are not named sequentially (i.e., prop1, prop2, prop3 etc.) but that they'll be set at the same time.

Thus, a solution would be:

// Store the variable names
var properties = [
    "firstProperty",
    "secondProperty",
    "propertyThree",
    "prop4",
    "prop5",
    "prop6",
    "seventhProp",
    "prop8",
    "prop9",
    "propTen",
    "propEleven",
    "property12"
];

var selection = quotes.slice(); // make a copy so that the original is not altered //

for (var i:Number = 0; i < properties.length; i++)
{
    var randomIndex = Math.floor(Math.random() * (selection.length + 1));

    // target is the object that holds the properties
    target[properties[i]] = selection.splice(randomIndex, 1);
}

Here's another way of doing it, that allows for setting properties on different objects:

var i = 0;
var randomQuotes = quotes.sort(function()
{
    return Math.round(Math.random() * 2) - 1;
});

target.prop = randomQuotes[i++];
target.prop2 = randomQuotes[i++];
other.prop = randomQuotes[i++];

// Keep going for all the properties you need to set

This could be abstracted away into a RandomQuote class, enabling you to re-use the functionality.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.