• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

storing a set of integers in an array

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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.
 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kae cee wrote:Hello,

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);
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say "it doesn't work"...what does happen?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you know your operator precedence? Specifically, which is higher - a cast, or multiplication?
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you tried instructions given by Mr. Abhinav Kumar Mishra,
check it once
reply
    Bookmark Topic Watch Topic
  • New Topic