• 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

Random method

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class RandomTest13
{
public static void main(String [] args)
{
for (int x=0; x < 15; x++)
System.out.print( (int)(Math.random()*10) + " " );
}

}

Hi All,
can anybody give the explanation as how the output is generated

thanks
venkat
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random numbers can be from 0.0 to 1.0 , 1.0 not inclusive

Hence a set of random numbers would be displayed beween 1 - 9 in value , depending on ur number of iterations
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi venkat,

-- (int)(Math.random()*10)

Math.random() -- this will generate random numbers between 0 and 1.

Ex: 0.8981130256628339 0.6494045787369 0.5306433259730561 0.8957299012137927

Math.random()*10 -- this will give
EX: 8.981130256628339 6.494045787369 5.306433259730561 8.957299012137927

(int)(Math.random()*10) -- this will type cast the values as int. so v ll get

Ex: 8 6 5 8

Make a note that random numbers will generate different values for every compile.

with regards,
Sethu.
reply
    Bookmark Topic Watch Topic
  • New Topic