Class RandomHelpers

java.lang.Object
com.apple.foundationdb.async.common.RandomHelpers

public final class RandomHelpers extends Object
  • Method Details

    • random

      @Nonnull public static SplittableRandom random(@Nonnull Tuple primaryKey)
      Returns a SplittableRandom seeded deterministically from a primary key. Distinct keys return distinct streams. Seeds the random deterministically from a record's primary key using 64 bits of entropy avoiding clashes.
      Parameters:
      primaryKey - the record's primary key
      Returns:
      a new SplittableRandom
    • random

      @Nonnull public static SplittableRandom random(@Nonnull UUID identity)
      Seed a SplittableRandom deterministically from a UUID identity (such as a task id), using all 128 bits into the seed.
      Parameters:
      identity - the UUID identity
      Returns:
      a new SplittableRandom
    • splitMixDouble

      public static double splitMixDouble(long x)
      Returns high-quality double hash code between 0 and 1. It uses splitMixLong(long) internally.
      Parameters:
      x - a long
      Returns:
      a high quality hash code of x as a double in the range [0.0d, 1.0d).
    • randomUuid

      @Nonnull public static UUID randomUuid(@Nonnull SplittableRandom random, boolean deterministicRandomness)
      Returns a fresh UUID: a reproducible one derived from random when deterministic randomness is requested, or a true random UUID.randomUUID() otherwise.
      Parameters:
      random - the random source to derive a reproducible UUID from when deterministicRandomness is set
      deterministicRandomness - whether to derive the UUID deterministically from random
      Returns:
      a new UUID
    • randomUuid

      @Nonnull public static UUID randomUuid(@Nonnull SplittableRandom random)
      Derives a type-4 (random) UUID from the given SplittableRandom, so that UUID generation can be made reproducible by seeding the source.
      Parameters:
      random - the random source to draw the UUID bits from
      Returns:
      a type-4 UUID derived from random
    • randomUuid

      @Nonnull public static UUID randomUuid(@Nonnull Tuple primaryKey, boolean deterministicRandomness)
      Returns a UUID for a record: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the primary key's encoded bytes when deterministic randomness is requested, or a random UUID.randomUUID() otherwise. Deriving the deterministic id straight from the full primary key (rather than from a low-entropy seeded stream) guarantees distinct records get distinct ids.
      Parameters:
      primaryKey - the record's primary key
      deterministicRandomness - whether to derive the UUID deterministically from primaryKey
      Returns:
      a new UUID
    • randomUuid

      @Nonnull public static UUID randomUuid(@Nonnull UUID identity, boolean deterministicRandomness)
      Returns a UUID: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the given UUID identity's bytes when deterministic randomness is requested, or a random UUID.randomUUID() otherwise.
      Parameters:
      identity - the UUID identity to derive from
      deterministicRandomness - whether to derive the UUID deterministically from identity
      Returns:
      a new UUID
    • forEach

      @Nonnull public static <T, U> CompletableFuture<List<U>> forEach(@Nonnull SplittableRandom splittableRandom, @Nonnull Iterable<T> items, @Nonnull BiFunction<T,SplittableRandom,CompletableFuture<U>> body, int parallelism, @Nonnull Executor executor)
      Applies an asynchronous body to each item, giving every invocation its own independent SplittableRandom split from splittableRandom so the work stays reproducible under deterministic randomness.
      Type Parameters:
      T - the type of input item
      U - the type of result produced per item
      Parameters:
      splittableRandom - the random source to split per item
      items - the items to process
      body - the asynchronous operation to apply to each item together with its own random stream
      parallelism - the maximum number of items to process concurrently
      executor - the executor to run on
      Returns:
      a future completing with the per-item results