random color prob.

heyy guys.. im new to this place..:cool: but i was hopin u could give me some answers on this(btw i dont know a whole lot about html.. just what ive picked up- im pretty much self-taught) ok heres my code: <br />
<html><br />
<head><br />
<script><br />
var all=new Array(6)<br />
all[0]="A"<br />
all[1]="B"<br />
all[2]="C"<br />
all[3]="D"<br />
all[4]="E"<br />
all[5]="F"<br />
var all2=new Array(9)<br />
all2[0]="1"<br />
all2[1]="2"<br />
all2[2]="3"<br />
all2[3]="4"<br />
all2[4]="5"<br />
all2[5]="6"<br />
all2[6]="7"<br />
all2[7]="8"<br />
all2[8]="9"<br />
var randalllet=Math.round(Math.random()*6)<br />
var randallnum=Math.round(Math.random()*9)<br />
var randcol2=new Array(6)<br />
randcol2[0]=all[randalllet]<br />
randcol2[1]=all[randallnum]<br />
randcol2[2]=all[randallnum]<br />
randcol2[3]=all2[randalllet]<br />
randcol2[4]=all2[randalllet]<br />
randcol2[5]=all2[randalllet]<br />
var randcol3=Math.round(Math.random()*6)<br />
var randcol=randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]<br />
document.write("<font size=7 color=\"#"+randcol+"\">hello</font>")<br />
</script><br />
</head><br />
<body><br />
</body><br />
</html><br />
<br />
<br />
the prob is that the colors seem to be in greyscale.. im not sure why.. prolly its an easy job to figure out but i sure cant.. i wrote the entire code myself based on somethin i saw somewhere else.. so im not suprised theres an error in it :D also if there is a way to shorten it that would be much appreciated<br />
<br />
thx<!--content-->just being picky first:<br />
<br />
1) <script> should be <script type='text/javascript'><br />
<br />
2) all lines should be delimited by ; for clarity.<br />
<br />
3) -<br />
<br />
<br />
randcol2[0]=all[randalllet]<br />
randcol2[1]=all[randallnum] // could be out of range of array<br />
randcol2[2]=all[randallnum] // could be out of range of array<br />
randcol2[3]=all2[randalllet]<br />
randcol2[4]=all2[randalllet]<br />
randcol2[5]=all2[randalllet]<br />
<br />
<br />
<br />
so you make a set of random hex digits in randcol2, then randomly selected one of those digits and repeat it 6 times:<br />
<br />
var randcol=randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]<br />
<br />
this will be grey because #xxxxxx where x is all equal is only an intensity.<br />
<br />
write this instead:<br />
<br />
var randcol=randcol2[0]+randcol2[1]+randcol2[2]+randcol2[3]+randcol2[4]+randcol2[5]<br />
<br />
<br />
this should help your code be error free, and create a pseudo-random colour.<!--content-->thx.. but now it isnt totally random because its always going to be a letter, a #, a #, a letter, a letter, and a letter making up the hex value right?<br />
<br />
<br />
<br />
oooohhh i see i made an error before- randcol2 should be:<br />
<br />
var randcol2=new Array(6)<br />
randcol2[0]=all[randalllet]<br />
randcol2[1]=all[randalllet]<br />
randcol2[2]=all[randalllet]<br />
randcol2[3]=all2[randallnum]<br />
randcol2[4]=all2[randallnum]<br />
randcol2[5]=all2[randallnum]<br />
<br />
but- that still doesnt solve the prob. and ur way it can't be ANY color- it has to have a certain order of letters and numbers.. like i said above<!--content-->var randcol3=Math.round(Math.random()*6)<br />
var randcol=randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]<br />
<br />
<br />
I think the prob is that it doesnt recognize 'randcol3' as a number.. could that be it? and if so how could i fix that? but on the other hand:<br />
<br />
var all=new Array(6)<br />
all[0]="A"<br />
all[1]="B"<br />
all[2]="C"<br />
all[3]="D"<br />
all[4]="E"<br />
all[5]="F"<br />
var all2=new Array(9)<br />
all2[0]="1"<br />
all2[1]="2"<br />
all2[2]="3"<br />
all2[3]="4"<br />
all2[4]="5"<br />
all2[5]="6"<br />
all2[6]="7"<br />
all2[7]="8"<br />
all2[8]="9"<br />
var randalllet=Math.round(Math.random()*6)<br />
var randallnum=Math.round(Math.random()*9)<br />
var randcol2=new Array(6)<br />
randcol2[0]=all[randalllet]<br />
randcol2[1]=all[randalllet]<br />
randcol2[2]=all[randalllet]<br />
randcol2[3]=all2[randallnum]<br />
randcol2[4]=all2[randallnum]<br />
randcol2[5]=all2[randallnum]<br />
<br />
<br />
<br />
<br />
in that case, it recognizes 'randalllet' and 'randallnum' as numbers, right? so why doesn't it in the other case?<!--content-->var randcol=randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol3]+randcol2[randcol<br />
3]+randcol2[randcol3]<br />
[/quote]<br />
my way was more random than what you have above. You're only going to pick out a single item in the randcol2 array, and repeat it 6 times. This is why you're getting grey.<br />
<br />
okay, to make it completely random:<br />
<br />
Code:
<br />
var all = "0123456789ABCDEF";<br />
<br />
var color = new array[6];<br />
<br />
for (var i = 0; i < 6; i++)<br />
{<br />
  var index = Math.round(Math.random()*15);<br />
<br />
  color[i] = all[index];<br />
}<br />
<br />
document.write("<font size=7 color=\"#"+color+"\">hello</font>")<!--content-->Okay, this is as simple as it can get:<br />
<br />
<script language="JavaScript" type="text/javascript"><!--<br />
var randcol='#';<br />
var opts='ABCDEF1234567890';<br />
while(randcol.length<7){<br />
    randcol+=opts.charAt(Math.floor(Math.random()*opts.length));<br />
}<br />
//--></script><!--content-->oooohhhh i get it now it only picks 1 random thing + repeats it.. tht makes sense im such a moron lol :) told ya i was a newb.. :D thx alot!!<!--content-->
 
Back
Top