# Randomize Variable

**URL:** <https://forum.kirupa.com/t/randomize-variable/185259>\
**Category:** flash\
**Created:** [April 12, 2006, 1:08am UTC](https://forum.kirupa.com/t/randomize-variable/185259 "2006-04-12T01:08:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyg3r](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tyg3r/32/1341_2.png) [@Tyg3r](https://forum.kirupa.com/u/Tyg3r)\
**Post date:** [April 12, 2006, 1:08am UTC](https://forum.kirupa.com/t/randomize-variable/185259/1 "2006-04-12T01:08:22Z")

</div>

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:

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

```

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

Any ideas?

* * *

[Iolite vaporizer](http://iolitevaporizer.net/)

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 12, 2006, 1:20am UTC](https://forum.kirupa.com/t/randomize-variable/185259/2 "2006-04-12T01:20:55Z")

</div>

yea

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Tyg3r](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tyg3r/32/1341_2.png) [@Tyg3r](https://forum.kirupa.com/u/Tyg3r)\
**Post date:** [April 12, 2006, 1:46am UTC](https://forum.kirupa.com/t/randomize-variable/185259/3 "2006-04-12T01:46:32Z")

</div>

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 = 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);  
}[/AS]

I hope this helps you, help me.

* * *

[Sell Vaporizers](http://vaporizeraffiliateprogram.com)

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 12, 2006, 2:23am UTC](https://forum.kirupa.com/t/randomize-variable/185259/4 "2006-04-12T02:23:36Z")

</div>

couldnt you just set a variable instead of tracing it

```auto

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);
}

```

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 12, 2006, 2:27am UTC](https://forum.kirupa.com/t/randomize-variable/185259/5 "2006-04-12T02:27:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tyg3r](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tyg3r/32/1341_2.png) [@Tyg3r](https://forum.kirupa.com/u/Tyg3r)\
**Post date:** [April 12, 2006, 11:41am UTC](https://forum.kirupa.com/t/randomize-variable/185259/6 "2006-04-12T11:41:15Z")

</div>

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. 😃

* * *

[Ferrari F430 Specifications](http://www.ferrari-wiki.com/wiki/Ferrari_F430)

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [April 12, 2006, 12:59pm UTC](https://forum.kirupa.com/t/randomize-variable/185259/7 "2006-04-12T12:59:06Z")

</div>

no worries mate :thumb:
