• NOW LIVE! Into the Woods--new character species, eerie monsters, and haunting villains to populate the woodlands of your D&D games.

Easy way to make tables for random choices in Javascript

Asmor

First Post
Here's something I whipped up really quickly. It's a Javsascript object that automates the task of getting a random entry from a table.

Code:
function getRandomItem() {
	roll=Math.floor(Math.random()*this.maxroll)+1
	inc=roll
	while (this.items[inc]==undefined) {
		inc++
	}
	return this.items[inc]
}

function randomTable(items) {
	this.items=items
	this.maxroll=items.length-1
	this.getItem=getRandomItem
}

The nice thing about this is that it allows you to quickly and easily make "tables" by using arrays. Take this table as an example, a random plot twist table from a PDF I bought.

1-2 Betrayal!
3-9 New Location
10 Greater Villain
11-13 Hidden Plot
14-15 Reversal!
16-18 Bizarre Occurance
19 Deus Ex Machina
20 Other

First you'd make an array, with each entry's index being its highest possible roll (i.e. New Location's would be 9, since a 9 is the highest number rolled which should be a new location)

Code:
twst=new Array()
twst[2]="Betrayal!"
twst[9]="New Location"
twst[10]="Greater Villain"
twst[13]="Hidden Plot"
twst[15]="Reversal!"
twst[18]="Bizarre Occurance"
twst[19]="Deus Ex Machina"
twst[20]="Other"

plotTwist=new randomTable(twst)

the last line creates one of the new objects from the array. You can then get a random item from that table with plotTwist.getItem().

I hope someone finds this useful. I love random generators!
 

log in or register to remove this ad

Into the Woods

Remove ads

Top