Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Method called Math.random() in Java

  1. Jan 2, 2012 #1
    Hello everyone.

    I am investigating this method and trying to find how random numbers are getting generated.
    Please look at the following code.

    Code (Text):
    class RandomNumbers{
    public static void main(String[] args) throws InterruptedException{
    int count=0;
    while(true){
    double d = Math.random();
    System.out.println(d);
    Thread.sleep(2000);
    count++;
    // Stop generating numbers when numbers generated are 20 in count
    if(count==20){
    System.exit(0);
    }
    }
    }
    }
    My intention is not trying to figure out how javasoft people wrote the code for this method. Maybe after reading my question, it might look as if I am trying to ask for the code in some sense, that's not my intention, at all.

    Question is: What is Math.random() method code keeping track of, if the procossor is generating some random numbers. All I can see is my computer CPU is getting power from my house hold socket and I write the Math.random() method in my program and CPU generates some random numbers and they are displayed on the screen.

    In broader sense, my question is something in the sense, where you can imagine a chemical reaction between two chemicals, which when combined, the atoms of the chemical combine and you get a different chemical, as a final product.

    What is happening inside the processor which the Math.random() method written by javasoft people is keeping track of? Is there any particular mathematical function implemented in the Math.random() method, but it doesn't make any sense, if you think about it. Or is Math.random() method is keeping track of fluctuation in voltage, and the variations in electrical energy supplied to the processor!?

    I know all of my assumptions are going to sound stupid after I get my answer.

    I hope my question is clear. Thanks!
     
    Last edited: Jan 2, 2012
  2. jcsd
  3. Jan 2, 2012 #2
    You're question is not very clear, but the answer is most likely here:
    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html [Broken]
    Otherwise, perhaps:
    http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random( [Broken])

    Random() bitshifts a seed value (default = the current time in milliseconds).
     
    Last edited by a moderator: May 5, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Method called Math.random() in Java
Loading...