Skip to content

Commit

Permalink
Merge pull request #19 from jhorneman/tracery2
Browse files Browse the repository at this point in the history
Added support for setting random number generator, to make Tracery deterministic
  • Loading branch information
Kate Compton committed Mar 29, 2016
2 parents 2c46aa7 + d8a7683 commit 70f69ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
5 changes: 4 additions & 1 deletion js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function runTests() {

grammar.addModifiers(baseEngModifiers);

Math.seedrandom(Math.random());
// Use fixed number instead of random.
// Math.seedrandom(Math.random());
tracery.setRng(function() { return 0; });

// Create all the test cases
var tests = {

Expand Down
12 changes: 10 additions & 2 deletions js/tracery/tracery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/

var tracery = function() {
var rng = Math.random;

var setRng = function setRng(newRng) {
rng = newRng;
};

var TraceryNode = function(parent, childIndex, settings) {
this.errors = [];
Expand Down Expand Up @@ -363,7 +368,7 @@ var tracery = function() {
break;
default:

index = Math.floor(Math.pow(Math.random(), this.falloff) * this.defaultRules.length);
index = Math.floor(Math.pow(rng(), this.falloff) * this.defaultRules.length);
break;
}

Expand Down Expand Up @@ -394,7 +399,7 @@ var tracery = function() {
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
randomIndex = Math.floor(rng() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
Expand Down Expand Up @@ -737,6 +742,9 @@ var tracery = function() {
tracery.Grammar = Grammar;
tracery.Symbol = Symbol;
tracery.RuleSet = RuleSet;

tracery.setRng = setRng;

return tracery;
}();

Expand Down
20 changes: 18 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Create an empty grammar:
grammar = tracery.createGrammar();

Create a grammar from a Tracery-formatted object:


grammar = tracery.createGrammar({origin:"foo"});

Add modifiers to the grammar (import "mods-eng-basic.js" for basic English modifiers, or write your own)
Expand Down Expand Up @@ -45,6 +45,19 @@ Get the root node from a rule *not* fully expanded (this allows for animating th
refreshGrammarOutput();
}, 40);


### Making Tracery deterministic

By default, Tracery uses Math.random() to generate random numbers. If you need Tracery to be deterministic, you can make it use your own random number generator using:

tracery.setRng(myRng);

where myRng is a function that, [like Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random), returns a floating-point, pseudo-random number in the range [0, 1).

By using a local random number generator that takes a seed and controlling this seed, you can make Tracery's behavior completely deterministic.

(Alternatively, you could use something like [seedrandom](https://github.com/davidbau/seedrandom) to make Math.random() seedable, but then you need to be very careful about who uses Math.random() - it effectively becomes a global variable that anyone can modify. Using a local random number generator - perhaps from seedrandom - instead of replacing Math.random() avoids this problem.)

## Library Concepts
### Grammar

Expand Down Expand Up @@ -113,4 +126,7 @@ Some attributes of this object can be:
* distribution: a new distribution to override the default)
* conditionRule: a rule to expand
* conditionValue: a value to match the expansion against
* conditionSuccess: a ruleset to use if expanding *conditionRule* returns *conditionValue*, otherwise use *baseRules* These can be nested, so it is possible to make a ruleset
* conditionSuccess: a ruleset to use if expanding *conditionRule* returns *conditionValue*, otherwise use *baseRules*


These can be nested, so it is possible to make a ruleset

0 comments on commit 70f69ed

Please sign in to comment.