如何指定随机数的范围?
我有随机插入数字的二叉搜索树代码.每次都可以修改大小,但我想修改数字的范围,例如:我希望随机数只有一位或两位.
I have binary search tree code that inserts numbers randomly. I can modify size each time, but I want to modify the range of numbers, for example: I want the random number be just one digit or just 2 digits.
我该怎么做?
public static void main( String[ ] args ) {
BinarySearchTree bst = new BinarySearchTree( );
Random random = new Random( System.currentTimeMillis() );
int[] randoms = new int[1000];
Random randGen = new Random();
for(int i = 0; i < randoms.length; i++)
{
bst.insert( random.nextInt( randoms.length ) );
}
System.out.println( "
sorted :" );
bst.inorderTraversal( );
bst.delete (4);
System.out.println( "
Max Value:" );
System.out.println(bst.maxValue());
System.out.println( "
Min Value:" );
System.out.println(bst.minValue());
System.out.println(bst.lookup(1));
}
推荐答案
怎么样:
只有一位数
just one digit
random.nextInt(10)
只有两位数
just 2 digits
random.nextInt(90) + 10
还是我误解了你的问题?
Or did I misunderstand your question?
相关文章