🌐
Ioflood
ioflood.com › blog › java-set
Java Set: Guide to Managing Collections of Elements
March 11, 2024 - Are you finding it challenging to work with Java Set? You're not alone. Many developers find themselves puzzled when it comes to handling Sets in Java, but
🌐
GeeksforGeeks
geeksforgeeks.org › arrays-in-java
Arrays in Java - GeeksforGeeks
1 month ago - A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Level Up Lunch
leveluplunch.com › java › examples › generate-sequential-numbers-from-range
Generate numbers from range | Level Up Lunch
March 22, 2014 - Using the Instrean.rangeClosed we will create a range of numbers between 20 and 30
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Set.html
Set (Java SE 11 & JDK 11 )
July 11, 2024 - The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience.
🌐
Mathbits
mathbits.com › JavaBitsNotebook › LibraryMethods › RandomGeneration.html
Java Random Generation - JavaBitsNotebook.com
Computers can be used to simulate the generation of random numbers. This random generation is referred to as a pseudo-random generation. These created values are not truly "random" because a mathematical formula is used to generate the values · The "random" numbers generated by the mathematical ...
🌐
Tutorialspoint
tutorialspoint.com › generate-a-random-array-of-integers-in-java
Generate a random array of integers in Java
Generate a random array of integers in Java - In order to generate random array of integers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() ...
🌐
www.javatpoint.com
javatpoint.com › how-to-generate-random-number-in-java
How to Generate Random Number in Java - Javatpoint
How to Generate Random Number in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.
🌐
Stack Overflow
stackoverflow.com › questions › 36568590 › how-to-create-a-sequence-of-numbers-in-java
How to create a sequence of numbers in java - Stack Overflow
StringBuilder sb = new StringBuilder();
for (int i=0; i<1000; i++) {
  sb.append(i);
}
System.out.println(sb.toString());

As a general note, while str = str + someString; will work, inside a loop it can quickly get out of hand. Try it with 10000 iterations and you'll see (large amounts of RAM & CPU consumed).

StringBuilder is better, if one really needs to build a string in this way, and it's always better performace-wise when one is appending to a character sequence more than a couple of times.

Answer from JimmyB on stackoverflow.com
🌐
Quora
quora.com › What-is-the-best-way-to-generate-random-numbers-from-multiple-patterns-in-Java
What is the best way to generate random numbers from multiple patterns in Java? - Quora
Answer (1 of 2): In Java, you can use the [code ]java.util.Random[/code] class to generate random numbers from multiple patterns. Here is an example of how to use this class to generate random integers within a certain range: [code]import java.util.Random; //create an instance of the Random cla...
🌐
Stack Overflow
stackoverflow.com › questions › 12756360 › how-to-make-java-set › 12756499
collections - How to make Java Set? - Stack Overflow

Like this:

import java.util.*;
Set<Integer> a = new HashSet<Integer>();
a.add( 1);
a.add( 2);
a.add( 3);

Or adding from an Array/ or multiple literals; wrap to a list, first.

Integer[] array = new Integer[]{ 1, 4, 5};
Set<Integer> b = new HashSet<Integer>();
b.addAll( Arrays.asList( b));         // from an array variable
b.addAll( Arrays.asList( 8, 9, 10));  // from literals

To get the intersection:

// copies all from A;  then removes those not in B.
Set<Integer> r = new HashSet( a);
r.retainAll( b);
// and print;   r.toString() implied.
System.out.println("A intersect B="+r);

Hope this answer helps. Vote for it!

Answer from Thomas W on stackoverflow.com
🌐
Novixys Software
novixys.com › blog › java-math-random-examples
Java Math.random Examples | Novixys Software Dev Blog
March 3, 2017 - Generate random numbers using Math.random() or Random. ThreadLocalRandom for multi-threaded access. SecureRandom generates random numbers for cryptography.
🌐
Baeldung
baeldung.com › home
Generating Random Numbers in Java | Baeldung
January 8, 2024 - Learn different ways of generating random numbers in Java.
🌐
Stack Overflow
stackoverflow.com › questions › 622 › most-efficient-code-for-the-first-10000-prime-numbers
performance - Most efficient code for the first 10000 prime numbers? - Stack Overflow

The Sieve of Atkin is probably what you're looking for, its upper bound running time is O(N/log log N).

If you only run the numbers 1 more and 1 less than the multiples of 6, it could be even faster, as all prime numbers above 3 are 1 away from some multiple of six. Resource for my statement

Answer from Matt on stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 47282057 › generate-string-permutations-from-multiple-set-values-java-8-streams
Generate String Permutations from multiple Set values (Java 8 Streams) - Stack Overflow

The following code creates all combinations from any number of input sets, ignoring null/empty sets:

Stream<Collection<String>> inputs = Stream.of(
        Arrays.asList("United States of america", "USA"),
        Arrays.asList("Texas", "TX"),
        Arrays.asList("Hello", "World"),
        null,
        new ArrayList<>());

Stream<Collection<List<String>>> listified = inputs
        .filter(Objects::nonNull)
        .filter(input -> !input.isEmpty())
        .map(l -> l.stream()
                .map(o -> new ArrayList<>(Arrays.asList(o)))
                .collect(Collectors.toList()));

Collection<List<String>> combinations = listified
        .reduce((input1, input2) -> {
            Collection<List<String>> merged = new ArrayList<>();
            input1.forEach(permutation1 -> input2.forEach(permutation2 -> {
                List<String> combination = new ArrayList<>();
                combination.addAll(permutation1);
                combination.addAll(permutation2);
                merged.add(combination);
            }));
            return merged;
        }).orElse(new HashSet<>());

combinations.forEach(System.out::println);

Output:

[United States of america, Texas, Hello]
[United States of america, Texas, World]
[United States of america, TX, Hello]
[United States of america, TX, World]
[USA, Texas, Hello]
[USA, Texas, World]
[USA, TX, Hello]
[USA, TX, World]

Now you can use your mentioned helper method to create the permutations of each combination. This question shows how to generate all permutations of a list.

Answer from Malte Hartwig on stackoverflow.com
🌐
Techie Delight
techiedelight.com › home › java › generate powerset of a set in java
Generate Powerset of a Set in Java | Techie Delight
September 14, 2022 - Write a program to generate Powerset of a set in Java. A power set of a set `S` is the set of all possible subsets of S, including the empty set and `S` itself.
🌐
Stack Overflow
stackoverflow.com › questions › 8115722 › generating-unique-random-numbers-in-java
Generating Unique Random Numbers in Java - Stack Overflow
  • Add each number in the range sequentially in a list structure.
  • Shuffle it.
  • Take the first 'n'.

Here is a simple implementation. This will print 3 unique random numbers from the range 1-10.

import java.util.ArrayList;
import java.util.Collections;

public class UniqueRandomNumbers {
    
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i=1; i<11; i++) list.add(i);
        Collections.shuffle(list);
        for (int i=0; i<3; i++) System.out.println(list.get(i));
    }
}

The first part of the fix with the original approach, as Mark Byers pointed out in an answer now deleted, is to use only a single Random instance.

That is what is causing the numbers to be identical. A Random instance is seeded by the current time in milliseconds. For a particular seed value, the 'random' instance will return the exact same sequence of pseudo random numbers.

Answer from Andrew Thompson on stackoverflow.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Random.html
Random (Java Platform SE 8 )
Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications. ... Creates a new random number generator. This constructor sets the seed of the random number generator to a value ...