Java- how to create a random hex value within a specified range of values

I have a script in a java web application where a random hex value should be generated. This value must be within the range of values ​​specified by me. (The range of values ​​can be hexadecimal or integer).

What is the most efficient way to do this> Should I generate a random decimal number and then convert it to hex? Or is it possible to directly generate a value?

+8
java decimal hex
source share
6 answers

Yes, you just generate a decimal value in your range. Something like:

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 

Heh and decimal numbers are treated the same in Java (as integers) and displayed or displayed differently. ( More on this .)

+13
source share

Try it,

 String s = String.format("%x",(int)(Math.random()*100)); System.out.println(s); 
+4
source share

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)); 
+1
source share
 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()); 
0
source share

Using Math.sin () (colors adapt 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] ); } 
0
source share

Random progressive six-color 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(); } 
-one
source share

All Articles