You can use the random () method in the math class to generate a value ranging from 0 ~ Random number between 1. This method is relatively simple. To become a requirement, the random class is added to the java. util package. It is a random number generator that can be initialized with 48 digits (seed, also called SEED.
For example:
Long seed = calendar. getinstance (). gettimeinmillis (); // when using the random class, you can use time-related values as random seeds.
Objrandom = new random (SEED );
Example:
Five double-digit exercise questions are randomly generated for primary school students.
Import java. util .*;
Public class genquestion
{
Random objrandom;
Public genquestion ()
{
Long seed = calendar. getinstance (). gettimeinmillis ();
Objrandom = new random (SEED );
}
Private void geneandprint ()
{
Stringbuffer buff = new stringbuffer ();
Buff. append ("the answer to the addition question is :");
For (INT I = 1; I <= 5; I ++)
{
Int A = objrandom. nextint (90) + 10;
Int B = objrandom. nextint (90) + 10;
System. Out. println (a + "+" + B + "=? ");
Buff. append (A + B );
Buff. append ("");
}
System. Out. println (buff. tostring ());
}
Public static void main (string ARGs [])
{
Genquestion OBJ = new genquestion ();
OBJ. geneandprint ();
}
}
NOTE: If two-digit addition is generated, the two numbers randomly generated must be between 10 and 10 ~ Within the range of 99. To obtain a random integer x, where a <= x <= B, apply "objrandom. Next (B-A + 1) + ".
The plus sign can be used for string connection. However, when a string is frequently connected, the use of the plus sign is less effective. We recommend that you use the stringbuffer class.