import java.util.Random;
import java.util.Scanner;
public class zhenshu {
public static void main(String[] args) {
System.out.print("请输入随机生成n bit位的长整数的长度:");
Scanner scanner=new Scanner(System.in);
int num= scanner.nextInt();
long random= createRndInteger(num);
System.out.println("随机生成的"+num+"bit位的长整数是:"+random);
}
public static long createRndInteger(int n){
int[] a={0,1};
StringBuilder SB=new StringBuilder();
Random random=new Random();
if(n==1){
return a[random.nextInt(a.length)];
}else{
SB.append(1);
for(int i=0;i<n-1;i++){
SB.append(a[random.nextInt(a.length)]);
}
System.out.println("随机生成的"+n+"bit位的二进制数是:"+SB.toString());
long num=Long.parseLong(SB.toString(),2);
return num;
}
}
}
结果: