# Random hex colours

**URL:** https://forum.kirupa.com/t/random-hex-colours/186562
**Category:** Uncategorized
**Created:** [April 22, 2006, 11:39am UTC](https://forum.kirupa.com/t/random-hex-colours/186562 "2006-04-22T11:39:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![willgoldstone7](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/willgoldstone7/32/2251_2.png) [@willgoldstone7](https://forum.kirupa.com/u/willgoldstone7)
#### Post date: [April 22, 2006, 11:39am UTC](https://forum.kirupa.com/t/random-hex-colours/186562/1 "2006-04-22T11:39:55Z")

</div>

is there a way to generate random hex colours using AS or do i need to define a set of hexs and then use a random var to determine which one is used?

---

<div class="post-metadata">

### Author: ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)
#### Post date: [April 22, 2006, 12:45pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/2 "2006-04-22T12:45:39Z")

</div>

```auto
r = Math.ceil(Math.random()*255);
r = (r<16) ? 0+r.toString(16) : r.toString(16);
g = Math.ceil(Math.random()*255);
g = (g<16) ? 0+g.toString(16) : g.toString(16);
b = Math.ceil(Math.random()*255);
b = (b<16) ? 0+b.toString(16) : b.toString(16);
trace("0x" + r + g + b);

```

---

<div class="post-metadata">

### Author: ![NinoScript](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ninoscript/32/1484_2.png) [@NinoScript](https://forum.kirupa.com/u/NinoScript)
#### Post date: [April 22, 2006, 2:01pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/3 "2006-04-22T14:01:04Z")

</div>

you can also use:  
random(0xFFFFFF);  
😉

---

<div class="post-metadata">

### Author: ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)
#### Post date: [April 23, 2006, 7:04pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/4 "2006-04-23T19:04:40Z")

</div>

or alternatively you could do it with non-depricated methods…

either…

Math.random() \* 0xFFFFFF;

or…

Math.round( Math.random() \* 0xFFFFFF );

Take Care.  
\_Michael

---

<div class="post-metadata">

### Author: ![willgoldstone7](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/willgoldstone7/32/2251_2.png) [@willgoldstone7](https://forum.kirupa.com/u/willgoldstone7)
#### Post date: [April 23, 2006, 9:19pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/5 "2006-04-23T21:19:44Z")

</div>

i don’t know what ‘non-depricated’ means but thanks. does it give any better performance?

---

<div class="post-metadata">

### Author: ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)
#### Post date: [April 23, 2006, 9:22pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/6 "2006-04-23T21:22:19Z")

</div>

Depricated functions are the old way of doing things, and eventually will be removed (in like four versions :P). The new way is more OOP…

---

<div class="post-metadata">

### Author: ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)
#### Post date: [April 23, 2006, 9:32pm UTC](https://forum.kirupa.com/t/random-hex-colours/186562/7 "2006-04-23T21:32:03Z")

</div>

Exactly, they are just methods that aren’t promised to be supported in future versions of the Flash Player.

\_Michael
