# Making random backgrounds more random w AS

**URL:** <https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798>\
**Category:** Uncategorized\
**Created:** [September 20, 2004, 5:52am UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798 "2004-09-20T05:52:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerosignal0](https://avatars.discourse-cdn.com/v4/letter/z/8edcca/32.png) [@zerosignal0](https://forum.kirupa.com/u/zerosignal0)\
**Post date:** [September 20, 2004, 5:52am UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/1 "2004-09-20T05:52:08Z")

</div>

Hello,  
Im working on my site and I have a random background script setup. Heres the code that Im using now.

```auto
 choice = Math.round(Math.random()*4);
 switch (choice) {
case 0 :
 _root.background.loadMovie("background1.swf");
 break;
case 1 :
 _root.background.loadMovie("background2.swf");
 break;
case 2 :
 _root.background.loadMovie("background5.swf");
 break;
case 3 :
 _root.background.loadMovie("background7.swf");
 break;
case 4 :
 _root.background.loadMovie("background8.swf");
 break;
} 

```

Now I love the way thats its working and but one thing that is kinda crappy is that sometimes it suffers from repeating the same background a couple of times before going to another random background. Now I know that its because the script is working properly but can anyone think of a method that I could use to make it not be able to pull up the last background that it has already loaded? I dont want to make it static because not only do I have this setup for when you enter the site but also its random when you click on one of the nav buttons because I want to try to keep my site dynamic.

Any ideas?  
Thanks in advance.

---

<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 20, 2004, 3:40pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/2 "2004-09-20T15:40:46Z")

</div>

make a do while loop that checks if the last random number was the same as the new one if it is just loop it again

```auto

do {
	choice = Math.round(Math.random()*4);
} while (zzz == choice); // check so the previous value that was spit out is not the same one it spits out this time
zzz = choice; // if it is makes it do the loop again, till it gets one that wasent same 
 

hope it helps =)

```

---

<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 20, 2004, 6:50pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/3 "2004-09-20T18:50:59Z")

</div>

Another way to do this

myArray = [“background1.swf”, “background2.swf”, “background3.swf”, “background4.swf”, “background5.swf”];  
function shuffle() {  
return Math.round(Math.random()\*2-1);  
}  
myArray.sort(shuffle);  
Then just loop through the array

---

<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 20, 2004, 7:16pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/4 "2004-09-20T19:16:53Z")

</div>

well I tried both of them but they dont seem to work… they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together… Im open to suggestions 😉  
thanks for responding by the way I appriciate it

---

<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 20, 2004, 7:31pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/5 "2004-09-20T19:31:03Z")

</div>

> [@zerosignal0](#):
>
> well I tried both of them but they dont seem to work… they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together… Im open to suggestions 😉  
> thanks for responding by the way I appriciate it

Not sure what you are doing with the backgrounds. If you want a different background each time a visitor returns to your site, you will have to have a look at sharedObject. But if you are changing the background periodically while a user is on the site, my code will work.

for example

myArray = [“background1.swf”, “background2.swf”, “background3.swf”, “background4.swf”, “background5.swf”];  
function shuffle() {  
return Math.round(Math.random()_2-1);  
}  
createEmptyMovieClip(“container”, 1);  
myArray.sort(shuffle);  
i = 0;  
function changebackground() {  
if (i\>=myArray.length) {  
i = 0;  
}  
loadMovie(myArray_, “container”);  
i++;  
}  
myInterval = setInterval(changebackground, 2000);

---

<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 20, 2004, 7:31pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/6 "2004-09-20T19:31:22Z")

</div>

r ur bkgrds swf’s just colored bkgrds?

> [@zerosignal0](#):
>
> well I tried both of them but they dont seem to work… they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together… Im open to suggestions 😉  
> thanks for responding by the way I appriciate it

---

<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 20, 2004, 8:01pm UTC](https://forum.kirupa.com/t/making-random-backgrounds-more-random-w-as/64798/7 "2004-09-20T20:01:32Z")

</div>

> [@ipaqflash](#):
>
> r ur bkgrds swf’s just colored bkgrds?

no you can see them here  
[www.zerosignaldesigns.com/site.htm](http://www.zerosignaldesigns.com/site.htm)
