[英]Generate Random numbers in array, add them together and store the answers in array
[英]Generate an amount of random numbers from a paramter then store them to call in a method later
我正在嘗試生成由參數確定的數量的隨機數,然后存儲這些數字,以便在例如用戶輸入代碼“HG-”和數字 4 之前,我可以將它們分配給與字符串代碼結合的 uniqe id 字段,該方法將輸出 HG -3981 或 HG-8394 等
我的嘗試
{
for (int i = 0; i == length; i++)
{
Random random = new Random();
int rand = random.nextInt((9 - 1) + 1) + 1;
}
System.out.println(prefix + "-" + length);
}
你不應該打印length
,而不是打印rand
變量嗎?
例子:
System.out.println(prefix+"-"+rand);
希望這有幫助!
這將生成 0 個填充length
數字整數:
Random rand = new Random();
int length = 6;
DecimalFormat df = new DecimalFormat();
df.setMaximumIntegerDigits(length);
df.setMinimumIntegerDigits(length);
df.setGroupingUsed(false);
for (int i = 0; i < 50; i++) {
System.out.println(df.format(rand.nextInt((int)Math.pow(10,length))));
}
您必須存儲這些以確保唯一性。
您可以使用Set<Integer>
、 int[10^length]
、 boolean[10^length]
或BitSet(10^lenght)
中的任何一個來記錄已生成的數字。 注意進程重啟等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.