How does Math.random() EXACTLY work in Java?

TheOddy Linux

I recently came across the

Math.random( )

class in Java. (And) I was wondering about the class. For example, I'm trying to write a dice game that needs random numbers from 1 to 6 each "roll". This is where math.random( ) comes in. This line of code:

int random_num = (int) (Math.random() * 6) + 1;

CAN generate random numbers between 1 to 6.

My problem lies at

(Math.random() * 6)

here.

I know how this code works, how Math.random() generates a double value between 0.0 to 1.0. And how it multiplied by 6, and was rounded in the end.

I was however, puzzled why a random number (say 0.78396954122) multiplied by 6 could become a random number between 1 to 6. Supposedly the random number 0.78396954122 multiplied by 6, is always 6, right? There's no way a 2.78396954122 can suddenly pop up!

I'm a complete noob at Java and would appreciate if you could help explain this.

Thanks...in advance!

T.J. Crowder

I was however, puzzled why a random number (say 0.78396954122) multiplied by 6 could become a random number between 1 to 6. Supposedly the random number 0.78396954122 multiplied by 6, is always 6, right?

No. 0.78396954122 * 6 is 4.70381724732 which when truncated by (int) becomes 4 which then becomes 5 when the + 1 is done.

Similarly, Math.random might return 0.2481654 which when multiplied by 6 is 1.4889924, which (int) truncates to 1, and then becomes 2 thanks to the + 1.

If it were, say, 0.0485847 * 6, that would be 0.2915082, which (int) truncates to 0, which becomes 1 thanks to the + 1.

The lowest value Math.random will ever return is 0.0, which is truncated to 0 by (int). 0 * 6 is 0, which becomes 1 after the + 1. The highest value Math.random will ever return is 0.99999999(etc) (e.g.,. it will be < 1). Multiplying that by 6 gives us 5.999999(etc) which is truncated to 5 by (int), and then turned into 6 by + 1.

So that's how we get the range 1-6 from that code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does Java.math.BigInteger.and() work?

From Dev

Switch() does not work with Math.random() numbers

From Dev

How does Integer.toString in Java exactly work?

From Dev

How exactly does the Java.reduce function with 3 parameters work?

From Java

How exactly does the callstack work?

From Java

How exactly does CMake work?

From Java

How does lock work exactly?

From Dev

How does keytab work exactly?

From Dev

How exactly does linking work?

From Dev

How exactly does the MenuInflater work?

From Dev

How does the << Operator exactly work?

From Dev

How does this for/in loop work exactly?

From Dev

How exactly does a proxy work?

From Dev

How exactly does linking work?

From Dev

How exactly does DNS work?

From Dev

How exactly does "/bin/[" work?

From Dev

How does this for/in loop work exactly?

From Dev

How does this function work exactly?

From Dev

Java Math.random() How random is it?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

Im making a program that makes random math, but it does not work

From Dev

How does List Comprehension exactly work in Python?

From Dev

numpy rollaxis - how exactly does it work?

From Dev

How exactly does link rel="preload" work?

From Dev

How exactly does the Hibernate @OneToMany annotation work?

From Java

How exactly does the Spring BeanPostProcessor work?

From Dev

how does ifstream readsome exactly work

From Dev

How exactly does Tomcat's Threadpool work

From Dev

How does git commit --amend work, exactly?