A Pseudo Random Generator in Java to Shuffle an Array/List

in #java5 years ago

Sometimes, we need to shuffle an array or list in Java - but we want to get the same "random" output for the same input. The following is a Pseudo Random Generator that is implemented in Java that shuffles an array or list using a seed.

The RANDOM_GENERATOR_NUMBER is a prime number (e.g. 999_999_000_001) and we use it to do some bit shifting and XOR. Then, we generate a random index to swap.

Prime numbers when factorization are using integers only 1 and itself. For example, prime number 999999000001 can only be represented as the product of 1 and 999999000001.

package helloacm.com;
 
import java.util.Arrays;
import java.util.List;
 
public class RandomGenerator<T> {
    private static long RANDOM_GENERATOR_NUMBER = 999_999_000_001L;
    public List<T> shuffle(List<T> list, long seed) {
        seed = seed << 32;
        final var sz = list.size();
        for (int i = 0; i < sz; i++) {
            long v = seed + i * RANDOM_GENERATOR_NUMBER;
            v = v ^ (v >> 12);
            v = v ^ (v << 25);
            v = v ^ (v >> 27);
            v = v * RANDOM_GENERATOR_NUMBER;
            int index = (int) (i + v % (sz - i));
            if (index >= 0 && index < sz) {
                T tmp = list.get(index);
                list.set(index, list.get(i));
                list.set(i, tmp);
            }
        }
        return list;
    }
}

For example, if you run the following program to shuffle an array of 7 numbers (1 to 7):

package helloacm.com
 
public class Main {
    public static void main(String[] args) {
        List<Integer> data = Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7});
        RandomGenerator<Integer> rand = new RandomGenerator<>();
        rand.shuffle(data, 1234);
        System.out.println(data);;
    }
}

It will always print:

[7, 5, 3, 4, 2, 6, 1]

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

Important Update of Delegation Service!

  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Sort:  

hello sir, I'm one your delegator, delegated 25K SP & set up you as witness voting proxy. I think you missed to upvote my first post after making delegation to you. Please, check it thoroughly--

https://steemit.com/hive-133716/@tanuja/chicken-roast

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.084
BTC 60162.63
ETH 1576.76
USDT 1.00
SBD 0.42