I just started learning JAVA and recently made a small game of guessing numbers on my own. It seems that obtaining four different random numbers is the key of this program. I previously searched for some methods to obtain random numbers on the Internet, but I am not familiar with JAVA, so I am not very familiar with them. As a result, I had the knowledge and thought of an algorithm for obtaining different random numbers.
I will select four random numbers from 0-9 as an example.
Int [] anArray = new int [10];
For (int I = 0; I <10; I ++)
{
AnArray [I] = I;
}
Int [] rand = new int [4];
For (int I = 0; I <4 ;)
{
Int j = (int) (Math. random () * 10 );
If (j <(10-I ))
{
Rand [I] = anArray [j];
AnArray [j] = anArray [9-i];
AnArray [9-i] = rand [I];
I ++;
}
Self-perception: this algorithm can be used to obtain different random numbers within a certain range. It is still relatively fast in terms of efficiency.
</