# Random color (setRGB)

**URL:** <https://forum.kirupa.com/t/random-color-setrgb/283>\
**Category:** flash\
**Created:** [July 12, 2002, 5:35am UTC](https://forum.kirupa.com/t/random-color-setrgb/283 "2002-07-12T05:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andr\_in](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/andr_in/32/37_2.png) [@andr\_in](https://forum.kirupa.com/u/andr_in)\
**Post date:** [July 12, 2002, 5:35am UTC](https://forum.kirupa.com/t/random-color-setrgb/283/1 "2002-07-12T05:35:09Z")

</div>

I’m trying to make a button that changes aa mc’s color to a random one from an array.  
The array:

```auto
mcarray = new Array();
mcarray = new Array(3);
mcarray = new Array("0x00FFFF", "0x00FF00", "0xFFFF00", "0x0000FF")

```

and the actions for a btn:

```auto
on (release) {
	mccolor = new Color(_root.mc);
	mccolor.setRGB(Math.random(mcarray));
}

```

But the mc turns black instead of some another color…  
I think it’s the “Math.random(mcarray)” there…  
or is it something else? :-\

Any help would be appreciated…

---

<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:** [July 12, 2002, 10:26am UTC](https://forum.kirupa.com/t/random-color-setrgb/283/2 "2002-07-12T10:26:24Z")

</div>

The thing is, the Math.random () function doesn’t take any parameter. It’s not a method that you can apply to an array. The way to select an element of an array would be myArray[0] for the first element.

The code for your button should the be

```auto
on (release) {
	myNum = Math.floor(Math.random()*3) ;
	mccolor = new Color(_root.mc);
	mccolor.setRGB(mcarray[myNum]);
}

```

Hope it helps.

pom  
:asian:

---

<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:** [July 12, 2002, 8:46pm UTC](https://forum.kirupa.com/t/random-color-setrgb/283/3 "2002-07-12T20:46:35Z")

</div>

here’s a shortcut for declaring your array:

```auto

mcarray = ["0x00FFFF", "0x00FF00", "0xFFFF00", "0x0000FF"];

```

and to make that button event more dynamic, haw about referencing the array length rather than spec’ing it statically?

```auto

on (release) {
	mccolor = new Color(_root.mc);
	mccolor.setRGB(mcarray[Math.floor(Math.random()*mcarray.length)]);
}

```

but as always, probably better to write a function or method:

```auto

MovieClip.prototype.setRandomColour = function(){
	var c = new Color(this);
	var a = _root.mcarray;
	c.setRGB(a[Math.floor(Math.random()*a.length)]);
}

```

then the button would read:

```auto

on(release){
	movieToChange.setRandomColour();
}

```

---

<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:** [July 13, 2002, 2:58pm UTC](https://forum.kirupa.com/t/random-color-setrgb/283/4 "2002-07-13T14:58:50Z")

</div>

woohoo! It works! You guys are great!  
That prototype thingy is pretty neat! 🙂

And I noticed that I don’t have to put  
**movieToChange** \*.setRandomColor();\*  
if the movie is defined in the function.
