# Need Help With Random Number Generation Between Upper and Lower Bound

Discussion in 'Java' started by ANM, Mar 6, 2004.

1. ### ANMGuest

Can someone recommend some code that I can reuse to generate random
numbers between an upper and lower bound in a range. Essentially, I
am looking for a function that will take in two integers as parameters
and then compute and return a random number that lies between that
range. Any help would be greatly appreciated. I am aware of the
Math.random method but, want to know if there is something better for
what I am trying to accomplish here for a school project.

ANM, Mar 6, 2004

2. ### Christophe VanfleterenGuest

ANM wrote:

> Can someone recommend some code that I can reuse to generate random
> numbers between an upper and lower bound in a range. Essentially, I
> am looking for a function that will take in two integers as parameters
> and then compute and return a random number that lies between that
> range. Any help would be greatly appreciated. I am aware of the
> Math.random method but, want to know if there is something better for
> what I am trying to accomplish here for a school project.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)

--
Kind regards,
Christophe Vanfleteren

Christophe Vanfleteren, Mar 6, 2004

3. ### Thomas SchodtGuest

Re: Need Help With Random Number Generation Between Upper and LowerBound

ANM wrote:
> /need to/ generate random
> numbers between an upper and lower bound in a range. Essentially, I
> am looking for a function that will take in two integers as parameters
> and then compute and return a random number that lies between that
> range.
> I am aware of the Math.random method
> but want to know if there is something better for
> what I am trying to accomplish here for a school project.

Your problem is conceptual.

Try to visualize what happens if you add a fixed offset (lower bound)
to the output of a random number generator method.

Adjust the range you "feed" the random number generator method accordingly.

Thomas Schodt, Mar 7, 2004