M0εiπ
M0εiπ

Reputation: 263

Are random Numbers really unPredictable in Java?

there is some ways to generate random Numbers in java

one of them is this:

Random rand=new Random();
int randomInteger=rand.nextInt();

now my question is this: can we predict next random Number?


edited after 4 answers:

my real problem is this:

I'm working on a Snake Game( nibbles in Linux) and I'm programing the snake to move, now I want to know if it's possible to Predict the next place that the apple will appear.

is it possible?

Upvotes: 2

Views: 2872

Answers (5)

Gowtham
Gowtham

Reputation: 1475

As others answer this question, it is possible to predict randomness of java.util.Random if you know the starting seed.

If you are working on a linux like system, take a look at these special files /dev/random and dev/urandom. Reads from these files are said to return "better" random numbers, the randomness depends on keyboard activity, mouse movement and some other exotic factors.

See this Wikipedia page for details. This page also says equivalent APIs exist in Windows.

Upvotes: 0

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

There could be NO really random numbers on deterministic devices like computer. But.

If you want a cryptographically secure random number, use SecureRandom: http://docs.oracle.com/javase/6/docs/api/java/security/SecureRandom.html

Random uses a deterministic algorithm:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#Random

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074266

You can not only predict it, but know it absolutely, if you know exactly what System.currentTimeMillis would return when you called new Random(). That's because new Random() is a shortcut for new Random(System.currentTimeMillis()), which sets the seed of a pseudo-random generator. (Well, that's what it did when I last looked at the source; the docs don't actually say it has to use that.) if you know the seed that new Random() used. Pseudo-random generators are deterministic, if you know the seed, you know the sequence. Update: Looking at the Java 6 source [I don't have Java 7 source handy], the default seed is a combination of a seed number that gets incremented on use, plus System.nanoTime. So you'd need to know both of those. Raises the bar.

If you don't know the exact value of System.currentTimeMillis() as of when new Random() occurs the seed used by new Random(), then it's very difficult indeed to predict what the next value will be. That's the point of pseudo-random generators. I won't say it's impossible. Just really, really hard to do with any degree of confidence.


Update after question edit: It's possible, but very, very hard, and in terms of doing so in a way that would allow a player to improve their score in the game, I'd say you can ignore it.

Upvotes: 5

Taymon
Taymon

Reputation: 25676

Essentially, if you know the seed of the random number generator, you can predict the entire sequence with certainty. If you don't, no matter how many numbers you generate, there's no way to accurately predict the next one.

Note that if you're relying on the numbers being unpredictable for security, you should be using java.secure.SecureRandom rather than java.util.Random.

Upvotes: 0

MattK
MattK

Reputation: 10273

The "random" numbers generated by the Random class are generated algorithmically, and as such are really pseudo-random numbers. So yes, in theory, you can predict the next number. Knowing one number that Random has produced, though, or even a series of numbers, isn't enough information to predict the next number; you would also need to know the seed that the Random object is using, and you would need to follow its pseudo-random number generation algorithm.

If you would like a repeatable set of "random" numbers, you can specify your own seed when creating an instance of Random, e.g.

Random rand = new Random(1234); // Replace 1234 with any value you'd like

Every time you instantiate Random with the same seed, you'll get the same series of numbers. So, for example, you could write a small command-line program that instantiates Random with some seed and prints a list of the numbers it returns, and then instantiate Random with the same seed in your code. Then you would know which numbers your code will receive and in what order. That's very handy for debugging.

Upvotes: 2

Related Questions