function RNG(n) { this.n = n; } RNG.prototype.method = function() { var n = 3; console.log(n, this.n); }; var rng = new RNG(7); rng.method(); // 3, 7
function RNG(n) { this.n = n; } RNG.prototype.method = function() { console.log(this.n); }; var rng = new RNG(7); rng.method(); // 7, within this method `this` is `rng`, so `this.n` gives you `rng.n`
function RNG(n) { this.n = n; } RNG.prototype.method = function() { console.log(n); }; var rng = new RNG(7); rng.method(); // ReferenceError: n is not defined
var ColourPicker = Class.create({ initialize: function() { this.hash = '#'; this.rngRed = new RNG(5872323); // using same seed always guarantees number order response same this.rngGreen = new RNG(332233); this.rngBlue = new RNG(3442178); }, numberToHex: function(number, padding) { var hex = number.toString(16); return hex; }, createColours: function(numColours) { var colours = []; for (var i = numColours - colours.length; i-- > 0;) { var r = this.rngRed.nextRange(5,240); var g = this.rngGreen.nextRange(10,250); var b = this.rngBlue.nextRange(0,230); colours.push(this.hash + this.numberToHex(r) + this.numberToHex(g) + this.numberToHex(b)); } return colours; } }); var colourPicker = new ColourPicker(); var colours = colourPicker.createColours(10); for (var i = 0 ; i < colours.length ; i++) { alert(colours[i]); }
public GapShuffler(T single) { rng = new RNG(); elements = new ArrayList<>(1); elements.add(single); size = 1; index = 0; indexSections = new int[][]{{0}}; } /**
/** * Creates a copy of this RNG; it will generate the same random numbers, given the same calls in order, as this RNG * at the point copy() is called. The copy will not share references with this RNG. * * @return a copy of this RNG */ @Override public RNG copy() { return new RNG(random.copy()); }
/** * Constructs a NaturalLanguageCipher that will use the given style of language generator to produce its text, using * the specified {@code shift} as a long to modify the generated words from the language's normal results. * @param language a FakeLanguageGen, typically one of the static constants in that class or a mix of them. * @param shift any long; this will be used to alter the specific words generated unless it is 0 */ public NaturalLanguageCipher(FakeLanguageGen language, long shift) { rs = new SemiRandom(0xDF58476D1CE4E5B9L + shift); rng = new RNG(rs); table = new UnorderedMap<>(512); reverse = new UnorderedMap<>(512); initialize(language, shift); }
/** * Constructor that takes any Collection of T, shuffles it with an unseeded RNG, and can then iterate infinitely * through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should * always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance * of a gap if every item in elements is unique, but that is not necessary and does not affect how this works. * @param elements a Collection of T that will not be modified */ public GapShuffler(Collection<T> elements) { rng = new RNG(new LongPeriodRNG()); this.elements = rng.shuffle(elements); size = this.elements.size(); double sz2 = size; index = 0; int portionSize = Math.min(20, Math.max(1, size / 2)); int minSection = Math.min(5, size / 2 + 1); while (size % portionSize < minSection && portionSize > 2) portionSize--; indexSections = new int[(int)Math.ceil(sz2 / portionSize)][]; for (int i = 0; i < indexSections.length - 1; i++) { indexSections[i] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations(portionSize)), portionSize, i * portionSize); sz2 -= portionSize; } indexSections[indexSections.length - 1] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations((int)sz2)), (int)sz2, (indexSections.length - 1) * portionSize); }
/** * Constructor that takes any Collection of T, shuffles it with an unseeded RNG, and can then iterate infinitely * through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should * always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance * of a gap if every item in elements is unique, but that is not necessary and does not affect how this works. * @param elements a Collection of T that will not be modified */ public GapShuffler(Collection<T> elements, String seed) { rng = new RNG(new LongPeriodRNG(seed)); this.elements = rng.shuffle(elements); size = this.elements.size(); double sz2 = size; index = 0; int portionSize = Math.min(20, Math.max(1, size / 2)); int minSection = Math.min(5, size / 2 + 1); while (size % portionSize < minSection && portionSize > 2) portionSize--; indexSections = new int[(int)Math.ceil(sz2 / portionSize)][]; for (int i = 0; i < indexSections.length - 1; i++) { indexSections[i] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations(portionSize)), portionSize, i * portionSize); sz2 -= portionSize; } indexSections[indexSections.length - 1] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations((int)sz2)), (int)sz2, (indexSections.length - 1) * portionSize); }
/** * Constructor that takes any Collection of T, shuffles it with an unseeded RNG, and can then iterate infinitely * through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should * always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance * of a gap if every item in elements is unique, but that is not necessary and does not affect how this works. * @param elements a Collection of T that will not be modified */ public GapShuffler(T[] elements, CharSequence seed) { rng = new RNG(new LongPeriodRNG(seed)); this.elements = Maker.makeList(rng.shuffle(elements)); size = this.elements.size(); double sz2 = size; index = 0; int portionSize = Math.min(20, Math.max(1, size / 2)); int minSection = Math.min(5, size / 2 + 1); while (size % portionSize < minSection && portionSize > 2) portionSize--; indexSections = new int[(int)Math.ceil(sz2 / portionSize)][]; for (int i = 0; i < indexSections.length - 1; i++) { indexSections[i] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations(portionSize)), portionSize, i * portionSize); sz2 -= portionSize; } indexSections[indexSections.length - 1] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations((int)sz2)), (int)sz2, (indexSections.length - 1) * portionSize); }
/** * Constructor that takes any Collection of T, shuffles it with an unseeded RNG, and can then iterate infinitely * through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should * always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance * of a gap if every item in elements is unique, but that is not necessary and does not affect how this works. * @param elements a Collection of T that will not be modified */ public GapShuffler(T[] elements) { rng = new RNG(new LongPeriodRNG()); this.elements = Maker.makeList(rng.shuffle(elements)); size = this.elements.size(); double sz2 = size; index = 0; int portionSize = Math.min(20, Math.max(1, size / 2)); int minSection = Math.min(5, size / 2 + 1); while (size % portionSize < minSection && portionSize > 2) portionSize--; indexSections = new int[(int)Math.ceil(sz2 / portionSize)][]; for (int i = 0; i < indexSections.length - 1; i++) { indexSections[i] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations(portionSize)), portionSize, i * portionSize); sz2 -= portionSize; } indexSections[indexSections.length - 1] = PermutationGenerator.decodePermutation( rng.nextLong(PermutationGenerator.getTotalPermutations((int)sz2)), (int)sz2, (indexSections.length - 1) * portionSize); }
rng = new RNG(rs); table = new UnorderedMap<>(other.table); reverse = new UnorderedMap<>(other.reverse);
/** * 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); } /**
RNG rng = new RNG(new LightRNG(seed)); int[] order = rng.randomOrdering(431); int o;
/** * 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); }