• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Math.random() method problem.

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

please any one can explain me why character 'z' is include
in the output.
public class ForLoop {
public static void main(String args[]) {
int character = 0;
int count = 0;
for(int a =0 ; a < 100 ; a++ ) {
character = (int) ( ( Math.random() * 26 ) + 65 );
// if Math.random() return maximam value
//0.9999999999999999999999 then
//0.9999999999999999999999 * 26 = 25.9999999999999999
// and finally 25.99999999999999999 + 65
// final maximam answer should be 90.9999999999999999999
// and its (int) is 90 so why z is include in the output
// if its ascii code is 91

System.out.print((char) character + " ");
count ++;
if (count == 10) {
count = 0;
System.out.println("");
}
}
}//end of main
}//end of class
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ASCII code for 'Z' is 90!
------------------
Alex J.Grig
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0.99999999999999 * 26 = 26
Just place it in a println to check it
 
Alex Grig
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But (int)(0.99999999999999 * 26) = 25

------------------
Alex J.Grig
 
Mr Iftikhar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alex Grig i got the thing.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic