/** * Generates a random permutation of the range from 0 (inclusive) to length (exclusive). * Useful for passing to OrderedMap or OrderedSet's reorder() methods. * * @param length the size of the ordering to produce * @return a random ordering containing all ints from 0 to length (exclusive) */ @Override public int[] randomOrdering(int length) { if (length <= 0) return new int[0]; return randomOrdering(length, new int[length]); }
/** * Assigns to dest an array of unique Coords, from (startX,startY) inclusive to (startX+width,startY+height) * exclusive, in a random order, with dest after this is called containing the lesser of {@code width * height} or * {@code dest.length} items. This will not allocate a new array for dest, but will create a temporary int array for * handling the shuffle. * * @param startX the inclusive starting x position * @param startY the inclusive starting y position * @param width the width of the space to place Coords in, extending from startX * @param height the height of the space to place Coords in, extending from startY * @param dest a Coord array that will be modified to contain randomly-ordered Coords, but will not be resized * @return dest, now with up to its first {@code width * height} items assigned to random Coords inside the given bounds */ public Coord[] getRandomUniqueCells(final int startX, final int startY, final int width, final int height, final Coord[] dest) { if (width <= 0 || height <= 0 || dest == null || dest.length <= 0) return dest; int[] o = randomOrdering(width * height); for (int i = 0; i < o.length && i < dest.length; i++) { dest[i] = Coord.get(startX + o[i] % width, startY + o[i] / width); } return dest; }
/** * Constructor that allows you to specify the adjective and noun collections used by * {@link #toWordMnemonic(int, boolean)} as well as a seed. This should be useful when you want to enforce a stable * relationship between word mnemonics produced by {@link #toWordMnemonic(int, boolean)} and the int values they * decode to with {@link #fromWordMnemonic(String)}, because the default can change if the adjective and noun * collections in {@link Thesaurus} change. There should be a fairly large amount of unique adjectives and nouns; * {@code (long)adjectives.size() * nouns.size() * adjectives.size() * nouns.size()} should be at least 0x80000000L * (2147483648L), with case disregarded. If the total is less than that, not all possible ints can be encoded with * {@link #toWordMnemonic(int, boolean)}. Having 216 adjectives and 216 nouns is enough for a rough target. Each * word (adjectives and nouns alike) can have any characters in it except for space, since space is used during * decoding to separate words. * @param seed a long seed that will be used to randomize the syllables and words used. * @param adjectives a Collection of unique Strings (case-insensitive) that will be used as adjectives * @param nouns a Collection of unique Strings (case-insensitive) that will be used as nouns */ public Mnemonic(long seed, Collection<String> adjectives, Collection<String> nouns) { RNG rng = new RNG(new LightRNG(seed)); int[] order = rng.randomOrdering(431); int o; for (int i = 0; i < 256; i++) { o = order[i]; items.add(baseTriplets.substring(o * 3, o * 3 + 3)); } allAdjectives.putAll(adjectives); allAdjectives.shuffle(rng); allNouns.putAll(nouns); allNouns.shuffle(rng); } /**
int[] order = rng.randomOrdering(431); int o; for (int i = 0; i < 256; i++) {
/** * Constructor for a Mnemonic generator that allows a different seed to be chosen, which will alter the syllables * produced by {@link #toMnemonic(long)} and the words produced by {@link #toWordMnemonic(int, boolean)} if you give * the same numeric argument to differently-seeded Mnemonic generators. Unless you know you need this, you should * probably use {@link #Mnemonic()} to ensure that your text can be decoded. * @param seed a long seed that will be used to randomize the syllables and words used. */ public Mnemonic(long seed) { RNG rng = new RNG(new LightRNG(seed)); int[] order = rng.randomOrdering(431); int o; for (int i = 0; i < 256; i++) { o = order[i]; items.add(baseTriplets.substring(o * 3, o * 3 + 3)); } for (int i = 0; i < adjective.size(); i++) { allAdjectives.putAll(adjective.getAt(i)); } allAdjectives.shuffle(rng); for (int i = 0; i < noun.size(); i++) { allNouns.putAll(noun.getAt(i)); } allNouns.shuffle(rng); }