Home java random numbers

random numbers




How in java language get a random number?

Answer 1, Authority 100%

import java.util.random;
// Initialize the generator
Random RND = New Random (System.CurrentTimemillis ());
// Get a random number in the MIN range to Max (inclusive)

function rnd.nextint (limit) Returns a number from zero (inclusive) to limit (not including LIMIT ).

Answer 2, Authority 36%

Option 1:

more complex and more functional. Returns a random number from 0 before indicated in brackets (not including). The type of returned number depends on the method used – nextint , nextdouble , nextfloat etc.

Random Random = New Random ();
int num = random.nextint (100);

If you need some particular interval of numbers, then do it.

Random Random = New Random ();
int num = 36 + random.nextint (100 - 36);

will return a random integer from 36 to 99.

option 2:

Simple, but less functional. Returns a random number of type Double from 0 to 1.

double num = math.random ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions