[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java initializing array with random numbers

Posted on 2011-04-28
23
Medium Priority
?
489 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Lambel
  • 8
  • 8
  • 3
  • +1
22 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35488967
yyou commented out assignment
0
 
LVL 92

Expert Comment

by:objects
ID: 35488970
due to rounding


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

Expert Comment

by:for_yan
ID: 35488971
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lambel
ID: 35488972
@for_yan: I did that for testing - if you run it, the output is all zeros(???)
0
 
LVL 92

Expert Comment

by:objects
ID: 35488973
> (int)Math.random()

that will always round to zero
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35488974
Yes rounding also true
but uncomment ykou need
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35488976
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
 
LVL 47

Expert Comment

by:for_yan
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]);
}
0
 

Author Comment

by:Lambel
ID: 35488987
@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
 
LVL 92

Expert Comment

by:objects
ID: 35488989
> @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
 
LVL 47

Expert Comment

by:for_yan
ID: 35488993
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35488999
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
 
LVL 92

Expert Comment

by:objects
ID: 35489012
That just repeats what I already posted??
0
 
LVL 92

Expert Comment

by:objects
ID: 35489015
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35490039
> @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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35490058
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35490059

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35490072
>>ID:35488999 also provided correct explanation

True - i actually missed that in all the noise ;)
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 35490428
> This is the code from  taken from  ID:35488981:

which does exactly the same as what I had already posted
0
 

Author Closing Comment

by:Lambel
ID: 35492320
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
 
LVL 92

Expert Comment

by:objects
ID: 35495038
Thanks Lynn :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question