Todor Balabanov
Todor Balabanov

Reputation: 388

Will I get better randomness by this way?

Will I get better random numbers quality if I will use such solution?

import java.util.*;

public class Main {
    private static final Random PRNGs[] = new Random[137];

    private static int nextInt() {
        return PRNGs[ (int)(System.currentTimeMillis() % PRNGs.length) ].nextInt();
    }

    static {
        for(int i=0; i<PRNGs.length; i++) {
            PRNGs[i] = new Random();
        }
    }

    public static void main(String[] args) {
        System.out.println( nextInt() );
    }
}

We know that in the real life it is possible to have 12 times red (or black) on the roulette. If one single instance of Random object is used such event as 12 times the same color is not achievable.

Upvotes: 0

Views: 85

Answers (1)

Jens
Jens

Reputation: 590

No, you do not get any better randomness. Have a look at SecureRandom If you want to use the random values e.g. for encryption. Random is quite predictable, whereas SecureRandom is not (that is the difference).

„Real“ random numbers need hardware support and are, to my knowledge, not readily available in Java.

As for „12 times red in a row“: the chances are very slim (assuming a 50/50 chance for red or black): 1/0.5^12 (1 in 4096) tries will give you 12 times red in a row.

Upvotes: 1

Related Questions