简体   繁体   English

用Java生成随机整数

[英]Generating a random int in Java

I have this code that if it does is generate random numbers in a range that I choose. 我有这段代码,如果这样做的话,会在我选择的范围内生成随机数。 The problem I have is that if they see the line of for, I supposedly have to generate 10000 random numbers, but this only makes me 6668. If I put a less than that amount, the code works fine. 我的问题是,如果他们看到for的行,则我必须生成10000个随机数,但这只能使我达到6668。如果我输入的数量少于该数量,则代码可以正常工作。 What's going on? 这是怎么回事? Eclipse has some sort of character limit? Eclipse有某种字符限制吗? Thank you very much! 非常感谢你!

Greetings! 问候!

import java.util.Random;
  public static final void main(String... aArgs){
log("Generating random integers in the range 1..10.");
    int START = 2000000 ;
int END =   8999999 ;
Random random = new Random();
for (int idx = 1; idx <= 10000; ++idx){
  showRandomInteger(START, END, random);
}

log("Done.");


}
private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
if (aStart > aEnd) {
  throw new IllegalArgumentException("Start cannot exceed End.");
    }
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
int randomNumber =  (int)(fraction + aStart);    
log("351" + randomNumber);


}
 private static void log(String aMessage){
System.out.println(aMessage);
  }
}

Yes, eclipse has a character limit on the console, but you can expand it or cancel the limit altogether. 是的,eclipse在控制台上有一个字符限制,但是您可以扩展它或完全取消该限制。

In Eclipse's preferences, go to Run/Debug , then to Console . 在Eclipse的首选项中,转到Run/Debug ,然后转到“ Console You see a checkbox "Limit console output". 您会看到一个复选框“限制控制台输出”。 You can uncheck it, or you can change the Console buffer size . 您可以取消选中它,也可以更改Console buffer size

I'm referring to Eclipse Luna. 我指的是Eclipse Luna。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM