Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中生成长随机整数_Java_Android_Android Studio - Fatal编程技术网

在Java中生成长随机整数

在Java中生成长随机整数,java,android,android-studio,Java,Android,Android Studio,我想找到一种方法来产生一个非常大的随机数。我在网上查了一下,但找不到我要找的东西。我需要生成一个介于1111111111111111111和99999999999999999之间的随机数;这个数字有22位长。 如果你知道怎么做,请告诉我。 谢谢我有一些我以前用过的东西 public class RandomString { private static final Random random = new Random(); public static String numeric(int co

我想找到一种方法来产生一个非常大的随机数。我在网上查了一下,但找不到我要找的东西。我需要生成一个介于1111111111111111111和99999999999999999之间的随机数;这个数字有22位长。 如果你知道怎么做,请告诉我。
谢谢

我有一些我以前用过的东西

public class RandomString {
private static final Random random = new Random();

public static String numeric(int count) {
    if (count == 0) {
        return "";
    } else if (count < 0) {
        throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
    }
    int end = 'z' + 1;
    int start = ' ';
    char[] buffer = new char[count];
    int gap = end - start;
    while (count-- != 0) {
        char ch;
        ch = (char) (random.nextInt(gap) + start);
        if (Character.isDigit(ch)) {
            if (ch >= 56320 && ch <= 57343) {
                if (count == 0) {
                    count++;
                } else {
                    // low surrogate, insert high surrogate after putting it in
                    buffer[count] = ch;
                    count--;
                    buffer[count] = (char) (55296 + random.nextInt(128));
                }
            } else if (ch >= 55296 && ch <= 56191) {
                if (count == 0) {
                    count++;
                } else {
                    // high surrogate, insert low surrogate before putting it in
                    buffer[count] = (char) (56320 + random.nextInt(128));
                    count--;
                    buffer[count] = ch;
                }
            } else if (ch >= 56192 && ch <= 56319) {
                // private high surrogate, no effing clue, so skip it
                count++;
            } else {
                buffer[count] = ch;
            }
        } else {
            count++;
        }
    }
    return new String(buffer);
   }
}

它可以做你想做的事

我有一些以前用过的东西

public class RandomString {
private static final Random random = new Random();

public static String numeric(int count) {
    if (count == 0) {
        return "";
    } else if (count < 0) {
        throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
    }
    int end = 'z' + 1;
    int start = ' ';
    char[] buffer = new char[count];
    int gap = end - start;
    while (count-- != 0) {
        char ch;
        ch = (char) (random.nextInt(gap) + start);
        if (Character.isDigit(ch)) {
            if (ch >= 56320 && ch <= 57343) {
                if (count == 0) {
                    count++;
                } else {
                    // low surrogate, insert high surrogate after putting it in
                    buffer[count] = ch;
                    count--;
                    buffer[count] = (char) (55296 + random.nextInt(128));
                }
            } else if (ch >= 55296 && ch <= 56191) {
                if (count == 0) {
                    count++;
                } else {
                    // high surrogate, insert low surrogate before putting it in
                    buffer[count] = (char) (56320 + random.nextInt(128));
                    count--;
                    buffer[count] = ch;
                }
            } else if (ch >= 56192 && ch <= 56319) {
                // private high surrogate, no effing clue, so skip it
                count++;
            } else {
                buffer[count] = ch;
            }
        } else {
            count++;
        }
    }
    return new String(buffer);
   }
}

它可以满足您的需求

以下是针对您需求的快速测试解决方案:

public void generateNumber(){

    BigInteger min = new BigInteger("1111111111111111111111");
    BigInteger max = new BigInteger("9999999999999999999999");

    System.out.println(random(min, max));
}

protected static Random RANDOM = new Random();
public static BigInteger random(BigInteger min, BigInteger max) {
    if(max.compareTo(min) < 0) {
        BigInteger tmp = min;
        min = max;
        max = tmp;
    } else if (max.compareTo(min) == 0) {
        return min;
    }
    max = max.add(BigInteger.ONE);
    BigInteger range = max.subtract(min);
    int length = range.bitLength();
    BigInteger result = new BigInteger(length, RANDOM);
    while(result.compareTo(range) >= 0) {
        result = new BigInteger(length, RANDOM);
    }
    result = result.add(min);
    return result;
}
public void generationEnumber(){
BigInteger最小值=新的BigInteger(“1111111111111111”);
BigInteger最大值=新的BigInteger(“99999999999999999”);
系统输出打印LN(随机(最小,最大));
}
受保护的静态随机=新随机();
公共静态BigInteger随机(BigInteger最小值、BigInteger最大值){
如果(最大值与(最小值)<0){
大整数tmp=min;
最小值=最大值;
max=tmp;
}否则如果(最大比较到(最小)==0){
返回最小值;
}
max=max.add(biginger.ONE);
BigInteger范围=最大值减去(最小值);
int length=range.bitLength();
BigInteger结果=新的BigInteger(长度,随机);
而(结果比较(范围)>=0){
结果=新的BigInteger(长度,随机);
}
结果=结果。添加(最小值);
返回结果;
}

以下是满足您需求的快速测试解决方案:

public void generateNumber(){

    BigInteger min = new BigInteger("1111111111111111111111");
    BigInteger max = new BigInteger("9999999999999999999999");

    System.out.println(random(min, max));
}

protected static Random RANDOM = new Random();
public static BigInteger random(BigInteger min, BigInteger max) {
    if(max.compareTo(min) < 0) {
        BigInteger tmp = min;
        min = max;
        max = tmp;
    } else if (max.compareTo(min) == 0) {
        return min;
    }
    max = max.add(BigInteger.ONE);
    BigInteger range = max.subtract(min);
    int length = range.bitLength();
    BigInteger result = new BigInteger(length, RANDOM);
    while(result.compareTo(range) >= 0) {
        result = new BigInteger(length, RANDOM);
    }
    result = result.add(min);
    return result;
}
public void generationEnumber(){
BigInteger最小值=新的BigInteger(“1111111111111111”);
BigInteger最大值=新的BigInteger(“99999999999999999”);
系统输出打印LN(随机(最小,最大));
}
受保护的静态随机=新随机();
公共静态BigInteger随机(BigInteger最小值、BigInteger最大值){
如果(最大值与(最小值)<0){
大整数tmp=min;
最小值=最大值;
max=tmp;
}否则如果(最大比较到(最小)==0){
返回最小值;
}
max=max.add(biginger.ONE);
BigInteger范围=最大值减去(最小值);
int length=range.bitLength();
BigInteger结果=新的BigInteger(长度,随机);
而(结果比较(范围)>=0){
结果=新的BigInteger(长度,随机);
}
结果=结果。添加(最小值);
返回结果;
}

您可以从
random
类中抽取22个随机单位数,将它们连接为
String
并将其转换为
BigInteger
您已经尝试和/或研究过的内容?您可以使用Math.round(Math.random()*(999999999999999999999999999999 l-1111111111111111111111111111111 l)之类的工具。但是,因为数字大于long的限制,我建议您使用字符串,如果您只想输出它或使用一个大整数进行计算。您可以从
random
类中随机抽取22个单位数,将它们连接为
String
并将其转换为
biginger
您已经尝试和/或研究了什么?您可以使用类似Math.round(Math.random()*(999999999999999999999999l-1111111111111111111111 l)+1111111111111111 l的东西。但是因为数字大于long的限制,我建议您使用一个字符串,如果您只想输出它,或者使用一个BigInteger来计算它。