Randomize Variable

Hi, im trying tell flash to randomly select a variable, but have no idea how to go about doing so.

Im trying to get it to pick one of the selected digits at random, for example:

time = "5","10","15","20"

Perhaps using various variables and then using arrays to select one. Eitherway, im stumped.

Any ideas?


Iolite vaporizer

yea


//create an array
time = [5, 10, 15, 20];
//select a random one
trace(time[Math.floor(Math.random()*time.length)]);

wow, quick responce. Cheers

Althouh it that code does actually randomly select a variable from the array, it still doesnt seem to be accomplishing my task.

Im using countdown timer sourse that will countdown a set number of seconds, then do something i.e. gotoAndPlay(2);

I have set the time to countdown from 0.10 (about 4 seconds) but Im trying to make it randomly countdown from one of 4 different times, and that code you just gave me doesnt seem to do the trick.

Original Count Down Timer:
[AS]time = 0.20

function CountDownTimer(durationMin) {
AsBroadcaster.initialize(this);
this.duration = durationMin601000;
// duration in ms.
this.startTime = getTimer();
this.id = setInterval(this, “checkTime”, 1000);
this.checkTime();
}
CountDownTimer.prototype.checkTime = function() {
if (getTimer()>=this.startTime+this.duration) {
this.broadcastMessage(“onComplete”);
clearInterval(this.id);
} else {
var timeLeft = this.duration-(getTimer()-this.startTime);
this.broadcastMessage(“onProgress”, timeLeft);
}
};
// usage
// textfield used to display the timer.
createTextField(“timerTF”, 0, 0, 0, 100, 100);
// create a 15 minutes countdown timer
myTimer = new CountDownTimer(time);
myTimer.addListener(this);
// handle time progress here
function onProgress(timeLeftMS) {
var totalSeconds = timeLeftMS/1000;
var hours = Math.floor(totalSeconds/3600);
var minutes = Math.floor((totalSeconds-((hours>0) ? 3600 : 0))/60);
var seconds = Math.floor(totalSeconds-((hours3600)+(minutes60)));
if (hours<10) {
hours = “0”+hours;
}
if (minutes<10) {
minutes = “0”+minutes;
}
if (seconds<10) {
seconds = “0”+seconds;
}
timerTF.text = hours+":"+minutes+":"+seconds;
}
// handle countdown completion here
function onComplete() {
gotoAndPlay(2);
}[/AS]

I hope this helps you, help me.


Sell Vaporizers

couldnt you just set a variable instead of tracing it


timeArray = [5, 10, 15, 20];
time = timeArray[Math.floor(Math.random()*timeArray.length)];

function CountDownTimer(durationMin) {
    AsBroadcaster.initialize(this);
    this.duration = durationMin*60*1000;
    // duration in ms.
    this.startTime = getTimer();
    this.id = setInterval(this, "checkTime", 1000);
    this.checkTime();
}
CountDownTimer.prototype.checkTime = function() {
    if (getTimer()>=this.startTime+this.duration) {
        this.broadcastMessage("onComplete");
        clearInterval(this.id);
    } else {
        var timeLeft = this.duration-(getTimer()-this.startTime);
        this.broadcastMessage("onProgress", timeLeft);
    }
};
// usage
// textfield used to display the timer.
createTextField("timerTF", 0, 0, 0, 100, 100);
// create a 15 minutes countdown timer
myTimer = new CountDownTimer(time);
myTimer.addListener(this);
// handle time progress here
function onProgress(timeLeftMS) {
    var totalSeconds = timeLeftMS/1000;
    var hours = Math.floor(totalSeconds/3600);
    var minutes = Math.floor((totalSeconds-((hours>0) ? 3600 : 0))/60);
    var seconds = Math.floor(totalSeconds-((hours*3600)+(minutes*60)));
    if (hours<10) {
        hours = "0"+hours;
    }
    if (minutes<10) {
        minutes = "0"+minutes;
    }
    if (seconds<10) {
        seconds = "0"+seconds;
    }
    timerTF.text = hours+":"+minutes+":"+seconds;
}
// handle countdown completion here
function onComplete() {
             gotoAndPlay(2);
}

also - i dont think “time” is a good name for a variable - think its actually part of AS syntax…

Your quite right on both acounts matthewjumps. I was rather tired last night and wasnt thinking straite. I changed the variable name from “time” to “mytimer” which solved a few issues.

As for that random timearray code… it worked like a charm. Thanx again, greatly appriciated. :smiley:


Ferrari F430 Specifications

no worries mate :thumb: