-
Senior Member
[RESOLVED] choose random color
I'm using the following code to randomly choose a color problem is the same color is being chosen too close togeather. does anybody know how to write this so that the colors are extremely randomly chosen? I know this is vauge but be creative
PHP Code:
var userCol =0x35E5ED
var ranCol = Math.floor(Math.random()*10-Math.random()*3);
trace(ranCol);
switch (ranCol) {
case -1 :
userCol =0x35E5ED;
break;
case 0 :
userCol =0x35E5ED;
break;
case 1 :
userCol =0x020C0C;
break;
case 2 :
userCol =0xFB4411;
break;
case 3 :
userCol =0x12F728;
break;
case 4 :
userCol =0x12D8F7;
break;
case 5 :
userCol =0x2709FD;
break;
case 6 :
userCol =0xEE00FF;
break;
case 7 :
userCol =0xCD23CB;
break;
case 8 :
userCol =0xED0E29;
break;
case 9 :
userCol =0x4F4546;
break;
case 10 :
userCol =0x454616;
break;
}
-
Based on the look of it, you want to randomly choose between a set number of colors?
Math.random() is really the only function, uh, hmm..
Nothing much really comes to mind. You can do some array creation, popping, etc to make sure no dupes come up and further flip the coin, but you're really stuck with computer "random" which really isn't.
Oh, and FYI this gives ranges below -1. Not sure if you're set up for that.
I (Love | Hate) Flash.
----
Save a version back so others may help you!
-
newb of many sorts
you could push each color into an array, then remove each one as it's served. that way you'll reduce the chance of getting the same color twice in a row...
Code:
var colors:Array = [0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF];
var holder:Array = [];
holder.push(colors.splice(Math.round(Math.random()*(colors.length-1)), 1));
var userCol:uint = holder[holder.length-1];
trace(userCol);
if(!colors.length){
colors = holder;
holder = [];
}
Otherwise, if you want a truly random color, then just generate some RGB values...
Code:
var r:uint = Math.floor(Math.random()*0xff);
var g:uint = Math.floor(Math.random()*0xff);
var b:uint = Math.floor(Math.random()*0xff);
var color:uint = r << 16 | g << 8 | b;
trace(color);
Last edited by Ralgoth; 12-30-2009 at 09:12 AM.
Reason: forgot a bracket
Search first, asked questions later.
-
Senior Member
thanks ralgoth i think i'll take your suggestion and create some random rgb colors didn't think of doing it that way.
-
Senior Member
Fabulous code Fabulous I say
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|