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
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);
}
yyou commented out assignment
due to rounding
System.out.println("Random num: " + (int)(Math.random() * 9.0));
System.out.println("Random
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);
}
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
}
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
that will always round to zero
Yes rounding also true
but uncomment ykou need
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
}
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
}
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?
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
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
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
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?
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));
It's nothing to do with rounding:
System.out.println("Random
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
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
( http:#35488970 )
>>
due to rounding
System.out.println("Random
>>
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]);
}
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
ID:35488999 also provided correct explanation
>>ID:35488999 also provided correct explanation
True - i actually missed that in all the noise ;)
True - i actually missed that in all the noise ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 to all for your help,
Lynn
Thanks Lynn :)