Reputation: 107
This line
g.setColor(new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256)));
generates a random color but I don't want to include black in it for some reason. Can I restrict values for example, just 80-256 to exclude the dark colors. I tried Math.random
and do modulo but it doesn't work. Please help me. Thank you very much!
Upvotes: 0
Views: 1068
Reputation: 1507
to generate numbers between 80-256:
css..
table {
font: 11px/24px Verdana, Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 320px;
}
td {
border: 1px solid #CCC;
padding: 0 0.5em;
}
html....
<head>
</head>
<body>
<div id='mydiv'></div>
<button id = "myRandomizeBtn">Randomize</button>
</body>
javascript...
$("#myRandomizeBtn").bind("click",randomizeHandler);
function randomizeHandler(evt){
var root=document.getElementById('mydiv');
var tab=document.createElement('table');
tab.className="mytable";
var tbo=document.createElement('tbody');
var row, cell;
var n = 3;
var data;
for (i=0; i<n; i++)
{
row=document.createElement('tr');
for(var j=0;j<n;j++){
cell=document.createElement('td');
data = Math.floor(80+(176*Math.random()));
cell.appendChild(document.createTextNode(data));
row.appendChild(cell);
}
tbo.appendChild(row);
}
tab.appendChild(tbo);
root.appendChild(tab);
}
here's a link to jsfiddle... hope it helps... http://jsfiddle.net/FhyHK/
Upvotes: 1
Reputation: 11543
If you want the to offset the number randomize in a smaller range and add the offset.
private int randomBetween(int min, int max) {
int range = max - min;
return min + numGen.nextInt(range);
}
...
g.setColor(new Color(randomBetween(80,256), randomBetween(80,256), randomBetween(80,256)));
This assumes you want a minimum intensity for each color component.
Upvotes: 2
Reputation: 168825
g.setColor(
new Color(127 + numGen.nextInt(128),
new Color(127 + numGen.nextInt(128),
new Color(127 + numGen.nextInt(128));
Upvotes: 1