Actionscript 3,生成随机数并放置在字节数组中,更快吗?

Actionscript 3 , generating random numbers and placing in byte array, faster?

我正在尝试生成随机字节的大字节数组。使用下面的代码,我可以生成 1000 万个随机字节并在大约 4 秒内放入一个字节数组中。 2 秒用于生成,2 秒用于放置在阵列上。

for (var i:Number = 0; i < reqLength; i++){
    rnd = Math.random() * 255;
    randomBytes.writeByte(rnd);
}

是否存在更快的方法?

我正在生成 ints 因为我正在创建一个扩展 ASCII 字符的字节数组。

通过一些调整,我将它从 4 秒调低到 0.5 秒。

var aBytes:ByteArray = new ByteArray;

// Set the final ByteArray length prior to filling it.
// It saves about 30% of elapsed time.
aBytes.length = 10000000;

// Write 2 500 000 x 4 byte unsigned ints instead of 10 000 000 x 1 bytes.
// You'll get 4 times less operations thus it is about 4 times faster.
for (var i:int = 0; i < 2500000; i++)
{
    // Don't use local variable = less operations.
    aBytes.writeUnsignedInt(Math.random() * uint.MAX_VALUE);
}

P.S。还有另一个有趣的选项,运行速度更快,比如 100 毫秒:

var aRaster:BitmapData = new BitmapData(5000, 500, true, 0x000000);
var aBytes:ByteArray = new ByteArray;

aRaster.noise(256 * Math.random());
aRaster.copyPixelsToByteArray(aRaster.rect, aBytes);