Creating a method and passing numbers

Hey everyone I am very new to java, learning only the basics in a computer class(high school).

I need to know how to create a method, and pass integers to it, in the form of a range (example:1-6). This method will be generating a random integer from this range. Any tips or examples would be appreciated, whole program code is not necessary.

Thanks,

Nick

[379 byte] By [x4ndera] at [2007-10-3 5:22:06]
# 1
A random number is Math.random()that won't give you an integer though so you need to do a Math.round() on it.If you want a number b/w 0 and 6 you wanna do something likeMath.round(Math.random() * 6); Something like that.
Norweeda at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...
# 2
A better solution is to use the java.util.Random class.
floundera at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...
# 3

If you want to pass a range, do it with two integers, like this:

myObject.myMethod(1, 6);

There's no "range" datatype in Java, although you could easily create one if you really needed it.

For example:

public class Range {

public final int start;

public final int end;

// rest left as exercise

}

I don't think you need that, however.

Use java.util.Random to get random numbers, including nextInt which gives you random ints in a range from 0 to some end point.Very useful for getting random positions from an array. If you want to change the range so it doesn't start just at zero, then add an offset.

paulcwa at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...
# 4
Everyone's always gotta have these easy solution :-)
Norweeda at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks for the tip, although i understand this way of generating an integer.The thing my teacher is stressing is the part about creating the method, and then passing the range to it from outside of the method(if this is possible).
x4ndera at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...
# 6

public int createRandomNumber(int low, int high) {

// generate a number between high and low

// using the Random class and return it

}

floundera at 2007-7-14 23:29:07 > top of Java-index,Java Essentials,New To Java...