I have a scenario in a java web app, where a random hexadecimal value has to be generated. This value should be within a range of values specified by me. (The range of values can be hexadecimal or integer values).
What is the most efficient way to do this> Do I have to generate a random decimal number, and then convert it to hexadecimal? Or can a value be directly generated?
Yes, you just generate a decimal value in your range. Something such as:
Random rand = new Random();
int myRandomNumber = rand.nextInt(0x10) + 0x10; // Generates a random number between 0x10 and 0x20
System.out.printf("%x\n",myRandomNumber); // Prints it in hex, such as "0x14"
// or....
String result = Integer.toHexString(myRandomNumber); // Random hex number in result
Hex and decimal numbers are handled the same way in Java (as integers), and are just displayed or inputted differently. (More info on that.)
Try this,
String s = String.format("%x",(int)(Math.random()*100));
System.out.println(s);
Random randomService = new Random();
StringBuilder sb = new StringBuilder();
while (sb.length() < RANDOM_HEX_LENGTH) {
sb.append(Integer.toHexString(randomService.nextInt()));
}
sb.setLength(RANDOM_HEX_LENGTH);
System.out.println(sb.toString());
Using Math.sin() (colors adaptable to totalItems to be colored):
double rFactor=0;
double gFactor=0.5;
double bFactor=1;
double rAdd=0.1;
double gAdd=0.2;
double bAdd=0.3;
String lettersLight[] = "6789ABCDEF".split("");
String lettersDark[] = "0123456789".split("");
int halfLetters=lettersDark.length/2;
private void initRandomColor2(int totalItems) {
double rFactor=0;
double gFactor=(Math.PI/totalItems)*2;
double bFactor=(Math.PI/totalItems)*4;
rAdd=(Math.PI/totalItems)+(Math.PI/totalItems);
gAdd=(Math.PI/totalItems)+(Math.PI/totalItems)*2;
bAdd=(Math.PI/totalItems)+(Math.PI/totalItems)*4;
}
private String getRandomColor2(boolean light) {
int r=(int)(halfLetters+(Math.sin(rFactor)*(halfLetters-1)));
int g=(int)(halfLetters+(Math.sin(gFactor)*(halfLetters-1)));
int b=(int)(halfLetters+(Math.sin(bFactor)*(halfLetters-1)));
rFactor+=rAdd;
gFactor+=gAdd;
bFactor+=bAdd;
return (light
?lettersLight[r]+lettersLight[r]+lettersLight[g]+lettersLight[g]+lettersLight[b]+lettersLight[b]
:lettersDark[r]+lettersDark[r]+lettersDark[g]+lettersDark[g]+lettersDark[b]+lettersDark[b]
);
}
You can try this. As this works for me:
Random random = new Random();
int nextInt = random.nextInt(256*256*256);
System.out.println(String.format("#%06x", nextInt));
Random progressive hex colors:
String letters[] = "0123456789ABCDEF".split("");
int min=letters.length-(letters.length/3);
int max=letters.length;
Random rnd=new Random(1000);
String colorEx[]= new String[]{"00","00","00"};
int colorChange=0;
int addColorChange=1;
private String getRandomColor() {
StringBuilder color = new StringBuilder("#");
int highColor=rnd.nextInt(2)+1;
for (int i = 0; i<3; i++) {
int addColor=0;
if (i==highColor)
highColor=min;
color.append(colorEx[i]);
if (colorChange==i) {
if (colorEx[i].equals("00"))
colorEx[i]="55";
else if (colorEx[i].equals("55"))
colorEx[i]="AA";
else if (colorEx[i].equals("AA"))
colorEx[i]="FF";
else {
if (i>0 && !"00".equals(colorEx[i-1]))
colorEx[i-1]="00";
else if (i<2)
colorEx[i+1]="00";
colorChange+=addColorChange;
//colorChange++;
if (colorChange>2 || colorChange<0) {
//colorChange=0;
addColorChange=-addColorChange;
colorChange+=addColorChange;
colorChange+=addColorChange;
}
}
}
}
return color.toString();
}