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

LambelAsked:
Who is Participating?
 
objectsCommented:
> This is the code from  taken from  ID:35488981:

which does exactly the same as what I had already posted
0
 
for_yanCommented:
yyou commented out assignment
0
 
objectsCommented:
due to rounding


System.out.println("Random num: " + (int)(Math.random() * 9.0));
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
for_yanCommented:
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);
}
0
 
LambelAuthor Commented:
@for_yan: I did that for testing - if you run it, the output is all zeros(???)
0
 
objectsCommented:
> (int)Math.random()

that will always round to zero
0
 
for_yanCommented:
Yes rounding also true
but uncomment ykou need
0
 
for_yanCommented:
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);
}
0
 
for_yanCommented:


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]);
}
0
 
LambelAuthor Commented:
@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?
0
 
objectsCommented:
> @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
0
 
for_yanCommented:
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
0
 
for_yanCommented:
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
0
 
objectsCommented:
That just repeats what I already posted??
0
 
objectsCommented:
> 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?
0
 
CEHJCommented:
> @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));
0
 
CEHJCommented:
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
0
 
for_yanCommented:

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
0
 
CEHJCommented:
>>ID:35488999 also provided correct explanation

True - i actually missed that in all the noise ;)
0
 
LambelAuthor Commented:
Sorry for the confusion.  I missed the meaning of your earlier comment until you pointed it out.

Thanks to all for your help,
Lynn
0
 
objectsCommented:
Thanks Lynn :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.