[JAVA] Random number creation (Math.random ();)

Random number creation

As part of my Java study, I needed a random number to create the answer when creating "Hit & Blow", so I will output a little of the knowledge I gained at that time.

[When you need a random number for the time being]

The "Hit & Blow" I created needed a number from 0 to 9, so I used the ** "Math class" ** below.

qiita.java



Math.random();

By using ** "Math class" ** in this way, you can get double type random numbers from 0.0 to less than 1.0.

For example

qiita.java



(int)(Math.random() * 10);

Random numbers from 0 to 9 can be obtained. (By the way, the first (int) is cast to int)

Summary

What did you think.

In this article, I introduced how to get random numbers by using ** "Math class" **.

Of course, there are other ways to get random numbers, so if you want to know more, please check it out.

Also, since I am still a beginner, I would appreciate it if you could point out any information due to incorrect notation or recognition in this article.

Recommended Posts

Random number creation (Math.random ();)
java (random number)
C # (random number) memo
[Java] Random number generation method (Random)
[Ruby] 4-digit random number generation with sprintf