Method called Math.random() in Java

  • Context: Java 
  • Thread starter Thread starter pairofstrings
  • Start date Start date
  • Tags Tags
    Java Method
Click For Summary
SUMMARY

The Math.random() method in Java generates pseudo-random numbers using a deterministic algorithm based on a seed value, which defaults to the current time in milliseconds. The underlying implementation utilizes the java.util.Random class, which employs bit-shifting techniques to produce a sequence of numbers. This method does not rely on external factors such as voltage fluctuations but rather on mathematical functions to ensure randomness. Understanding this mechanism is crucial for developers seeking to implement random number generation effectively in their applications.

PREREQUISITES
  • Java programming language fundamentals
  • Understanding of pseudo-random number generation
  • Familiarity with the java.util.Random class
  • Basic knowledge of bit manipulation techniques
NEXT STEPS
  • Explore the java.util.Random class documentation
  • Learn about different random number generation algorithms
  • Investigate the impact of seed values on randomness
  • Study the performance implications of Math.random() versus java.util.Random
USEFUL FOR

Java developers, software engineers, and anyone interested in understanding random number generation in programming contexts.

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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
Replies
22
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
7
Views
22K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K