Languages
[Edit]
EN

Java - generate random boolean with seed (the same random results each time when I create new instance)

3 points
Created by:
Root-ssh
115400

Qucik solution:

import java.util.Random;

public class RandomWithSeed {
    private final Random random;

    public RandomWithSeed(int seed) {
        this.random = new Random(seed);
    }

    public boolean nextBoolean() {
        return random.nextBoolean();
    }

    // example:
    public static void main(String[] args) {
        {
            RandomWithSeed randomWithSeed = new RandomWithSeed(218756092);

            System.out.println("# First run:");
            for (int i = 0; i < 5; i++) {
                System.out.println(randomWithSeed.nextBoolean());
            }
        }

        {
            RandomWithSeed randomWithSeed = new RandomWithSeed(218756092);

            System.out.println("# Second run:");
            for (int i = 0; i < 5; i++) {
                System.out.println(randomWithSeed.nextBoolean());
            }
        }
    }
}

Output:

# First run:
true
false
true
false
true
# Second run:
true
false
true
false
true

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join