Java的如何值的指定范围内产生一个随机十六进制值(java- how to generate a

2019-06-25 14:45发布

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

什么是最有效的方式做到这一点>我必须生成一个随机十进制数,然后将其转换为十六进制? 或者可以在值直接生成?

Answer 1:

是的,你刚才生成的范围内的十进制值。 一些诸如:

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中(为整数),并只显示或输入不同。 ( 上更多信息 。)



Answer 2:

试试这个,

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


Answer 3:

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


Answer 4:

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


Answer 5:

你可以试试这个。 由于这对我的作品:

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


Answer 6:

随机渐进十六进制颜色:

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


文章来源: java- how to generate a random hexadecimal value within specified range of values
标签: java hex decimal