Question

Is it possible to have an array like from 1 to 5, and using Math.Random but instead of only having a result like 1, 4, 3, 2, 5 is it possible to have a string using the numbers 1 to 5 but duplicated until the string contains 20 values by having the numbers 1 to 5 duplicated?

E.g. [1,4,2,5,3,1,2,3,4,5,2,1,3,4,5,2,3,3,2,1]

Was it helpful?

Solution

The way I understand is that you want to create a random string or array using predefined values:

var possibleValues:Array = [1, 2, 3, 4, 5];
var outputLength:uint = 20;
var outputString:String = "";
var outputArray:Array = [];

for(var i:uint = 0; i<outputLength; i++) {
    outputString += possibleValues[Math.floor(Math.random() * possibleValues.length)];
    outputArray.push(possibleValues[Math.floor(Math.random() * possibleValues.length)]);
}

trace(outputString); //15343412344135121512
trace(outputArray); //2,2,3,1,2,2,1,3,5,4,2,2,3,3,2,1,1,4,1,5

OTHER TIPS

Slightly incoherent question, but try:

var A:Array = new Array();

for (var i = 0; i < 20; i++)
    {
      var j:Number = Math.floor(Math.random()*5)+1
      A.push(j)
    }

trace (A);

By the way the example you gave was not a 'String' but an array full of numbers. An array full of strings would look something like ["1", "2", "3", ...].

And an array with 1 long string: ["123456..."]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top