Link to home
Start Free TrialLog in
Avatar of Lambel
Lambel

asked on

java initializing array with random numbers

I am trying to initialize an array with random numbers.  Can't figure out why it's putting out zero's. Any ideas??
Lynn


int numItems = 20;
int[] array = new int[numItems];

for(int j = 0;j< array.length; j++){
  //array[j] = (int)Math.random() * 9;
System.out.println("Random num: " + (int)Math.random() * 9); 
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

yyou commented out assignment
Avatar of Mick Barry
due to rounding


System.out.println("Random num: " + (int)(Math.random() * 9.0));
Try this:
int numItems = 20;
int[] array = new int[numItems];

for(int j = 0;j< array.length; j++){
 array[j] = (int)Math.random() * 9;
System.out.println("Random num: " + (int)Math.random() * 9);
}
Avatar of Lambel
Lambel

ASKER

@for_yan: I did that for testing - if you run it, the output is all zeros(???)
> (int)Math.random()

that will always round to zero
Yes rounding also true
but uncomment ykou need
But please post it correctly - it distracts

int numItems = 20;
int[] array = new int[numItems];

for(int j = 0;j< array.length; j++){
 array[j] = (int)(Math.random() * 9);
System.out.println("Random num: " + (int)Math.random() * 9);
}


int numItems = 20;
int[] array = new int[numItems];

for(int j = 0;j< array.length; j++){
 array[j] = (int)(Math.random() * 9);
System.out.println("Random num: " + array[j]);
}
Avatar of Lambel

ASKER

@objects:  I thought it was a rounding problem, but it switched out 9 for 100, and it still gave me zeros.
What do I need to do to come up with random integers?
> @objects:  I thought it was a rounding problem, but it switched out 9 for 100, and it still gave me zeros.

the rounding has already occurred before that. its rounding the random to zero
try my first comment
Math.random()  generated=s from 0 to 1
when you cast it integer it always bcekl=omes zzero

Multiply firts then cast and it will be OK
If you cast a number between 0 and less than 1 to integer ut will use the floor
of that number - it will be zero

if you first multiply by 10 or by 100 then the floor will be integer and you can
cast and get integer number
That just repeats what I already posted??
> Thanks for your help - I used another set of () and that fixed it.

which is what I suggested in my first comment.
Why accept a comment that repeats what had already been posted?
> @objects:  I thought it was a rounding problem, but it switched out 9 for 100, and it still gave me zeros.

It's nothing to do with rounding:

System.out.println("Random num: " + (int)Math.random() * 9);

You cast the result of random() to an int, which will result in zero - game over.

What you need is the following, which will cast the result of random() * 9 to an int

System.out.println("Random num: " + (int)(Math.random() * 9));
Oh and it's nothing to do with widening/narrowing conversions either, which is why specifying 9.0 makes no difference to the below, since 9 will already be widened to a double automatically

( http:#35488970 )
>>
due to rounding


System.out.println("Random num: " + (int)(Math.random() * 9.0));
>>

The problem is due to

a. casting
b. precedence

The answer had been assigned correctly.

This is the code from  taken from  ID:35488981:

           int numItems = 20;
int[] array = new int[numItems];

for(int j = 0;j< array.length; j++){
 array[j] = (int)(Math.random() * 9);
System.out.println("Random num: " + array[j]);
} 

Open in new window


THis is the output of random numbers (by no means all zeros):

Random num: 5
Random num: 2
Random num: 4
Random num: 8
Random num: 0
Random num: 3
Random num: 3
Random num: 8
Random num: 4
Random num: 3
Random num: 5
Random num: 7
Random num: 8
Random num: 1
Random num: 1
Random num: 5
Random num: 6
Random num: 5
Random num: 0
Random num: 5

Open in new window


ID:35488999 also provided correct explanation
>>ID:35488999 also provided correct explanation

True - i actually missed that in all the noise ;)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lambel

ASKER

Sorry for the confusion.  I missed the meaning of your earlier comment until you pointed it out.

Thanks to all for your help,
Lynn
Thanks Lynn :)