# random numbers

Discussion in 'Java' started by Ams, Feb 23, 2005.

1. ### AmsGuest

hi,
I am writing a method to invoke random numbers. I need the numbers to
be in the range 0-1000 but the numbers that i have created are huge
with around 7 digits. Does anyone no how or if its even possible to do
this??
Would greatly appreciate any help...

Ams, Feb 23, 2005

2. ### TechBookReportGuest

Ams wrote:
> hi,
> I am writing a method to invoke random numbers. I need the numbers to
> be in the range 0-1000 but the numbers that i have created are huge
> with around 7 digits. Does anyone no how or if its even possible to do
> this??
> Would greatly appreciate any help...

Look at the nextInt(int n) method, this returns a random int between 0
and n-1.

HTH
==============================================================
TechBookReport Java: http://www.techbookreport.com/JavaIndex.html

TechBookReport, Feb 23, 2005

3. ### MalteGuest

Ams wrote:
> hi,
> I am writing a method to invoke random numbers. I need the numbers to
> be in the range 0-1000 but the numbers that i have created are huge
> with around 7 digits. Does anyone no how or if its even possible to do
> this??
> Would greatly appreciate any help...

Hmm, it's been a while, but something like this:

int bigRandom;
int myRandom;

myRandom = (bigRandom%1000) + 1;

?

Malte, Feb 23, 2005
4. ### Alun HarfordGuest

"Ams" <> wrote in message
news:...
> hi,
> I am writing a method to invoke random numbers. I need the numbers to
> be in the range 0-1000 but the numbers that i have created are huge
> with around 7 digits. Does anyone no how or if its even possible to do
> this??
> Would greatly appreciate any help...

java.util.Random random = new java.util.Random();
int requiredRandomNumber = random.nextInt(1001);

Alun Harford

Alun Harford, Feb 23, 2005