Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Use of Math.random

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the use of Math.random in the following and also in general :
int x=(int)(Math.random() * 5)
Can anyone explain.
Thanks
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out
Marilyn
[This message has been edited by Marilyn deQueiroz (edited September 26, 2000).]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
close Marilyn, you left out the text. Clickey the edit icon and see how I did it.


[This message has been edited by Apu Nahasapeemapetilon (edited September 28, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math.random() //generates a random number.
Math.random * 5;//multiplies it by five
(int)Math.random * 5;
//does type conversion to int, incase it may be of float type
int y = (int)Math.random * 5
// stores the value to y
if i am not mistaken Math.random generates random numbers between the range of 0 to 1. (but just check up on that again)
cheers
ak.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct. Math.random() generates a random number from 0 to 1 with equal probability each time (uniform distribution).
[This message has been edited by Brien Hackney (edited October 12, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic