# Scripting : Open a random file

**URL:** <https://forum.kirupa.com/t/scripting-open-a-random-file/63711>\
**Category:** flash\
**Created:** [September 9, 2004, 11:42am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711 "2004-09-09T11:42:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RayDream](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@RayDream](https://forum.kirupa.com/u/RayDream)\
**Post date:** [September 9, 2004, 11:42am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/1 "2004-09-09T11:42:36Z")

</div>

Hi,

I’m kind of a newbie to scripting… I’m intending to use flash to serve as a navigation menu on my website to open a random file to a specific frame.

on (press) {  
getURL(“test1.htm”, “mainFrame”);  
}

However, how can I make it in such a way that it will randomly pick “test1.htm”, “test2.htm” or “test3.htm”?

Thank you!  
(The last time I was here was last year to ask some flash question, thanks to Eilsoe who solved my problem!)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2004, 1:42am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/2 "2004-09-10T01:42:30Z")

</div>

setup variables on the main timeline:  
whichfile=Math.round(Math.random()\*3+1); // pick a number between 1 and 3, round it off.  
savefile=whichfile;

for the button:[AS]on(release){  
getURL(“test”+\_root.whichfile+".htm", “mainFrame”);  
\_root.savefile=\_root.whichfile; // remember which file  
trace(\_root.whichfile); // which file should be opening  
\_root.whichfile=Math.round(Math.random()\*3+1);  
if ((\_root.whichfile==\_root.savefile) and (\_root.whichfile!=3));{  
whichfile+=1;  
}  
else if ((\_root.whichfile==\_root.savefile) and (\_root.whichfile==3)){  
\_root.whichfile=Math.round(Math.random()\*2+1);  
}  
}[/AS]

hope that works.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2004, 2:49am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/3 "2004-09-11T02:49:13Z")

</div>

Hey it works great! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2004, 2:55am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/4 "2004-09-11T02:55:32Z")

</div>

no problem.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2004, 3:30am UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/5 "2004-09-11T03:30:11Z")

</div>

Just some question… How can I change the number of random files chosen? What does the ‘if, whichfile+=1;’ and ‘else if, \_root.whichfile = Math.round(Math.random()\*2+1);’ mean?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2004, 2:31pm UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/6 "2004-09-11T14:31:03Z")

</div>

> [@RayDream](#):
>
> Just some question… How can I change the number of random files chosen? What does the ‘if, whichfile+=1;’ and ‘else if, \_root.whichfile = Math.round(Math.random()\*2+1);’ mean?

sorry, i didn’t really comment that code. so here it is commented.[AS]on(release){  
getURL(“test”+\_root.whichfile+“.htm”, “mainFrame”);  
\_root.savefile=\_root.whichfile; // remember which file  
trace(\_root.whichfile); // which file should be opening  
\_root.whichfile=Math.round(Math.random()\*3+1); // select new file randomly  
if ((\_root.whichfile==\_root.savefile) and (\_root.whichfile!=3));{ // if newly selected file is repeat of old file but not the last file in group  
whichfile+=1; // add one to new file  
}  
else if ((\_root.whichfile==\_root.savefile) and (\_root.whichfile==3)){ // if newly selected file is repeat of old file and is last file in group  
\_root.whichfile=Math.round(Math.random()\*2+1); reselect new file randomly from a range of first file to (last file-1)  
}  
}[/AS]basically, that ending bit of code ensures that files aren’t repeated. the savefile variable just lets us compare our new value for whichfile to our old value. if it’s a repeat, we reselect a new whichfile, either by adding 1 or doing it randomly again.

to change the total number of files, find the 3’s in there and make them whatever number you want (ie 10 for 10 files). even better would be to replace all the 3’s with \_root.totalfiles . then in the main timeline, setup a variable named totalfiles=10; this would make it easier to change in the future (because instead of changing all the 3’s, you’d only be changing one variable on the main timeline). you would also have to change the \_root.whichfile=Math.round(Math.random()_2+1); part of the button AS to \_root.whichfile=Math.round(Math.random()_(totalfiles-1)+1);

// i don’t know what’s going on, but in the AS just above, preview post is showing me a space between the L and E of totalfiles. there should be no space. it won’t let me change it for whatever reason.

if repeating doesn’t matter to you, you could take out everything after the //select new file randomly (remember to keep the last } though).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2004, 7:40pm UTC](https://forum.kirupa.com/t/scripting-open-a-random-file/63711/7 "2004-09-11T19:40:51Z")

</div>

Understood, thanks for your help and advices!
