Java Method called Math.random() in Java

  • Thread starter Thread starter pairofstrings
  • Start date Start date
  • Tags Tags
    Java Method
AI Thread Summary
The discussion revolves around understanding how the Math.random() method in Java generates random numbers. The user is curious about the underlying mechanisms that produce these random values, questioning whether it involves tracking CPU power fluctuations or if it relies on a mathematical function. The response clarifies that Math.random() uses a pseudorandom number generator, which typically starts with a seed value, often based on the current time in milliseconds. This method does not generate truly random numbers but rather produces numbers that appear random through deterministic algorithms. The user is directed to official Java documentation for further details on the Random class and the Math.random() method.
pairofstrings
Messages
411
Reaction score
7
Hello everyone.

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

Code:
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:
Technology news on Phys.org
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
Otherwise, perhaps:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#random( )

Random() bitshifts a seed value (default = the current time in milliseconds).
 
Last edited by a moderator:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top