java-如何在指定的值范围内生成随机十六进制值

时间:2012-06-19 05:23:17

标签: java hex decimal

我在java Web应用程序中有一个场景,必须生成一个随机的十六进制值。该值应在我指定的值范围内。 (值的范围可以是十六进制或整数值。)

最有效的方法是什么>我是否必须生成随机十进制数,然后将其转换为十六进制?或者可以直接生成值吗?

6 个答案:

答案 0 :(得分:18)

是的,您只需在您的范围内生成小数值。例如:

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

十六进制和十进制数在Java中以相同的方式处理(作为整数),并且只是以不同方式显示或输入。 (More info on that。)

答案 1 :(得分:4)

试试这个,

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

答案 2 :(得分:1)

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());

答案 3 :(得分:1)

使用Math.sin()(颜色适合于totalItems着色):

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]
            );
}

答案 4 :(得分:1)

你可以试试这个。因为这适合我:

Random random = new Random();
int nextInt = random.nextInt(256*256*256);
System.out.println(String.format("#%06x", nextInt));

答案 5 :(得分:0)

随机渐进十六进制颜色:

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();
}