Method called Math.random() in Java

  • Java
  • Thread starter pairofstrings
  • Start date
  • Tags
    Java Method
In summary: The seed is used to generate the next random number based on a predefined algorithm. In summary, the Math.random() method generates random numbers by bitshifting a seed value and using a predefined algorithm to produce the next number in a sequence, which appears random to the user.
  • #1
pairofstrings
411
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
  • #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
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:

1. What is the purpose of using the method called Math.random() in Java?

The method called Math.random() in Java is used to generate a random number between 0.0 and 1.0. This method is often used in programs that require a random element, such as games or simulations.

2. How does the method called Math.random() work?

The method called Math.random() uses a mathematical algorithm to generate a random number. It does this by multiplying a random number by a large number and then taking the remainder when divided by 1. This results in a number between 0.0 and 1.0.

3. Can the method called Math.random() be used to generate a specific range of numbers?

Yes, the method called Math.random() can be used to generate a specific range of numbers by using mathematical operations. For example, to generate a random number between 1 and 10, you can use the formula (int)(Math.random() * 10) + 1.

4. Is the method called Math.random() truly random?

The method called Math.random() is not truly random, as it is based on a mathematical algorithm. However, the generated numbers are unpredictable and appear to be random, making it suitable for most applications that require a random element.

5. Can the method called Math.random() be used for cryptography or security purposes?

No, the method called Math.random() should not be used for cryptography or security purposes. It is not truly random and can be easily predicted, making it unsuitable for secure applications. Instead, a cryptographically secure random number generator should be used for these purposes.

Similar threads

  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
803
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top