This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

I'm trying to generate 50 random integers between 0 and 10 using a 'for' construct and the 'Math.random()' method. I know how to do this, but I don't know how to store all these integers in an array (one dimension). I really only know how to store integers by manually inputting them into the array. I haven't made much progress and could use some help.

I'm trying to generate 50 random integers between 0 and 10 using a 'for' construct and the 'Math.random()' method. I know how to do this, but I don't know how to store all these integers in an array (one dimension). I really only know how to store integers by manually inputting them into the array. I haven't made much progress and could use some help.

This was my attempt (it doesn't work):

Thanks.

hi

In the above code double value returned by random() function is converted into int before multiplication with 11.So we have to put the statement as
array[j]=(int)(Math.random()*11);

do you know your operator precedence? Specifically, which is higher - a cast, or multiplication?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Khair Bahir
Greenhorn

Joined: Aug 01, 2011
Posts: 14

posted

0

Yeah, I know now that casts take higher precedence than multiplication operators, which is why the program just displayed a bunch of zeroes when I omitted a set of parentheses. Thanks for your guys' help.