Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

math.random() follows which algorithms

Tags:

java

random

Am using math.random() method to generate random numbers. but i had a doubt about that method. math.random() is which algorithms will fallow to generate random numbers. Is there any another random number generated algorithm?

Am trying this code but i think this is not efficient to generate random code:

for (int i = 0; i < N; i++) {
     int sd = i + (int) (Math.random() * (N-i));

     String t = one[r];
     one[r] = one[i];
     one[i] = t;
 }

Is there any better algorithm for random number generation?

like image 751
user3289828 Avatar asked Mar 25 '14 07:03

user3289828


4 Answers

Java mainly provides four random number generator API's depending of your use case.

java.lang.Math.random()

If we check the Math class source code, we can view this:

private static Random randomNumberGenerator;

private static synchronized void initRNG() {
    if (randomNumberGenerator == null)
        randomNumberGenerator = new Random();
}

public static double random() {
    if (randomNumberGenerator == null) initRNG();
    return randomNumberGenerator.nextDouble();
}

Math.random() is simply a shortcut to call Random Class. It is more simple and less complete than java.util.Random but it's enough in some cases.

java.util.Random

The Random class implement a Linear Congruential Generator.

LCG is a pretty simple formula for Pseudorandom Number Generation. java.util.Random is not trully random, it is totally deterministric. With the same initial condition (also called the seed), you've got the same result in the same order.

Use java.util.Random is good for the most use cases (simulation, games, ...) but is not good for cryptography because of his predictability, for this kind of use case prefer java.security.SecureRandom.

java.util.Random is thread safe but can have performance issues in multi-threaded context. If you work in a multi-threaded application, prefer ThreadLocalRandom.

java.security.SecureRandom

The SecureRandom class extend java.util.Random class to implement a cryptographically strong random number generator based on an entropy source. SecureRandom is not deterministic.

SecureRandom have multiple implementation in function of your platform (the complete implementation list).

java.security.SecureRandom is less fast than java.util.Random because of entropy source.

java.util.concurrent.ThreadLocalRandom

The ThreadLocalRandom class is another implementation of Linear Congruential Generator but this one isn't thread-safe but dedicated to a specific thread.

This implementation is more fast than java.util.Random in multi-threaded context.


In your case, you could use java.util.Collections.shuffle(list) to shuffle your array with java.util.Random or with a specific Random Generator like java.security.SecureRandom.

like image 99
Valentin Michalak Avatar answered Sep 22 '22 22:09

Valentin Michalak


Use java.util.Random API

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)

Algorithm class : pseudorandom number generator known as PRNG. You can read more about it here.

Note : Math.random() also uses java.util.Random instance to generate the psuedo-random numbers internally

like image 32
Keerthivasan Avatar answered Sep 23 '22 22:09

Keerthivasan


It uses the pseudo random number generation algorithms ie the numbers which are constantly fluctuating from throughout the system.

You may also find this interseting to read:- Pseudo-Random vs. True Random

Also check the Source for java.util.Random

like image 30
Rahul Tripathi Avatar answered Sep 23 '22 22:09

Rahul Tripathi


The simplest way to do what you are trying to do is

String[] words = ...
Collections.shuffle(Arrays.asList(words));

You are right that generating a random double and then turning it into a small integer is not efficient. Fortunately Random has a method for this.

Random rand = new Random();
for (int i = words.length - 1; i > 0; i--) {
     int sd = rand.nextInt(i+1); // random number between 0 and i inclusive

     String t = words[r];
     words[r] = words[i];
     words[i] = t;
}
like image 31
Peter Lawrey Avatar answered Sep 26 '22 22:09

Peter Lawrey