• Please note: In an effort to ensure that all of our users feel welcome on our forums, we’ve updated our forum rules. You can review the updated rules here: http://forums.wolflair.com/showthread.php?t=5528.

    If a fellow Community member is not following the forum rules, please report the post by clicking the Report button (the red yield sign on the left) located on every post. This will notify the moderators directly. If you have any questions about these new rules, please contact support@wolflair.com.

    - The Lone Wolf Development Team

More Fun with Random Tables

jkthomsen9

Well-known member
I am hoping that the community can help me out again. In the Hungry Storm AP there is a random table with values 1-150. If you are in region 1 you add nothing, region 2 add +25, region 3 add +50. How could this best be accomplished? I was thinking three bottons, one per region, but have no idea how to add to the random number generated if you push button 2, or 3.

Here is what I have so far.




<HTML>
<HEAD>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Crown of the World Caravan Encounters</TITLE>
</HEAD>
<BODY>

<script language="JavaScript">

// Some Arrays


rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
}

function rand(number) {
return Math.ceil(rnd()*number);
}

function tableResults(argument, sectionIdentifier) {

var varisiaArray = [];

varisiaArray.push({"max": 5, "name": "Rough Terrain", "content": "a" });
varisiaArray.push({"max": 10, "name": "Cabin", "content": "b" });
varisiaArray.push({"max": 15, "name": "Erutaki Encampment", "content": "c" });
varisiaArray.push({"max": 20, "name": "Hungry Predators", "content": "d" });
varisiaArray.push({"max": 25, "name": "Horned Herd", "content": "e" });
varisiaArray.push({"max": 30, "name": "Wanderers", "content": "f" });
varisiaArray.push({"max": 35, "name": "Wrecked Caravan", "content": "g" });
varisiaArray.push({"max": 40, "name": "Dragon Sighting", "content": "h" });
varisiaArray.push({"max": 50, "name": "Blizzard", "content": "i" });
varisiaArray.push({"max": 55, "name": "Aurora", "content": "j" });
varisiaArray.push({"max": 60, "name": "Wings of Hope", "content": "k" });
varisiaArray.push({"max": 65, "name": "Death from Below", "content": "l" });
varisiaArray.push({"max": 70, "name": "Creeping Rot", "content": "m" });
varisiaArray.push({"max": 76, "name": "Wagon Damage", "content": "n" });
varisiaArray.push({"max": 80, "name": "Ice Hunters", "content": "o" });
varisiaArray.push({"max": 85, "name": "Pleasant Weather", "content": "p" });
varisiaArray.push({"max": 90, "name": "Polar Pudding", "content": "q" });
varisiaArray.push({"max": 100, "name": "Frozen Dead", "content": "r" });
varisiaArray.push({"max": 105, "name": "Black Monolith", "content": "s" });
varisiaArray.push({"max": 110, "name": "The Lonely Maiden", "content": "t" });
varisiaArray.push({"max": 115, "name": "Northern Lights", "content": "u" });
varisiaArray.push({"max": 120, "name": "Rough Terrain", "content": "v" });
varisiaArray.push({"max": 125, "name": "Crevasse", "content": "w" });
varisiaArray.push({"max": 130, "name": "Polar Mirage", "content": "x" });
varisiaArray.push({"max": 135, "name": "Black Slush", "content": "y" });
varisiaArray.push({"max": 140, "name": "Auroral Flight", "content": "z" });
varisiaArray.push({"max": 145, "name": "Cold Gate", "content": "aa" });
varisiaArray.push({"max": 150, "name": "Hunting Party", "content": "ab" });



var allArrays = {
"varisia": varisiaArray,
};

var randomNum = rand(100) + 1;
var arrayOfChoices = allArrays[argument];
var error = "";
if (!arrayOfChoices) {
error = "no such set: " + argument;
}


var content = "";
var name = "";
for (var itemIndex in arrayOfChoices) {
var item = arrayOfChoices[itemIndex];
if (randomNum <= item.max) {
content = item.content;
name = item.name
break;
}
}

var results = [];
if (error) {
results.name = "";
results.content = error;
} else {
results.name = name;
results.content = content;
}
return results;

}

</script>

<div id="error"></div>
<form>
<input type="button" name="Button1" value="Outer Rim" onClick="var x = tableResults('varisia');this.form.name.value=x.name;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button2" value="High Ice" onClick="var x = tableResults('varisia');this.form.name.value=x.name;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button3" value="Boreal Expanse" onClick="var x = tableResults('varisia');this.form.name.value=x.name;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>
 
you could have three separate random numbers rolled.

Region 1 button would roll 1 to 100
Region 2 button would roll 26 to 125
Region 3 button would roll 51 to 150

then just look up on the table.

Alternatively, but more complicated, you could have three different tables all three would have a range from 1 to 100. The first table would have the normal 1 to 100 items. The second table would include items 26 to 100 as items 1 through 75 and then items 76 to 100 would be items 101 through 125 off the master table. Table three would have items 51 through 150 on it taking up the slots 1 through 100. Basically, each table of 100 items would be a subset of the master table.
 
this would give you the rolls that Viking suggested, based on your button.... you would only have one method still.

it would mean region3 could never have a number less than 51, an region 1 could never have a number > 100

-------------------------------------------


ok, so you have sectionIdentifier (i assume thats region)

your new random method should simply be:

var randomNum = rand(100) + 1 + ((sectionIdentifier -1) * 25)



inside your inputs it should be:

tableResults('varisia',1)

or
tableResults('varisia', 2)

etc...

im not at realms works, but that should basically do what you need.
 
Last edited:
Should the rand(100) actually be rand(99)?

The old random number generators I've worked with generated a number from zero to the value. zero to 100 with a +1 afterwards would give you a range from 1 to 101 and not 1 to 100. Unless of course they've changed that. I haven't done any real programming in years.
 
Thank you for the code. :)

I think some writers just like messing with us.... I'm working on one for wandering monsters in Stoneheart Valley. Table entries for encounters are 1 to 13. Roll 1d6, +1 in hills, +2 in low mountain, +3 in mountains, +2 in forests and +2 if at night.

I like their approach as the weaker encounters are (mostly) at the lower end of the table. A roll of (2) results in 2d4 goblins with a leader versus a roll o (12) 1d3 wyverns or (13) a lesser gibbering orb. The further off the beaten path you get, the more dangerous it becomes.
 
i copied it from the first post, and you're partially right. Get rid of the +1

the 100 is correct though.
 
Last edited:
by the way, for a little less random, but still ery good, and quite readable:

you can change the method rand to be

function rand(x) {
return Math.ceil(Math.random() * x);
}

Its the Math.ceil that makes it from 1-100 as opposed 0-99

And in my opinion, Math.random is good enough for us for this. (more readable too)
 
this would give you the rolls that Viking suggested, based on your button.... you would only have one method still.

it would mean region3 could never have a number less than 51, an region 1 could never have a number > 100

-------------------------------------------


ok, so you have sectionIdentifier (i assume thats region)

your new random method should simply be:

var randomNum = rand(100) + 1 + ((sectionIdentifier -1) * 25)



inside your inputs it should be:

tableResults('varisia',1)

or
tableResults('varisia', 2)

etc...

im not at realms works, but that should basically do what you need.

Mirtos I believe you wrote this code on the first table lookup. I have no code training. I have learned enough that I can sorta tell what a section is doing but can not write any on my own. Rather I cut and past bits and pieces to try and get the desired result.

I am not sure what a sectionIdentifier is. looking at the code it only appears once...function tableResults(argument, sectionIdentifier) {. Wouldn't that have to be linked to the buttons somehow?

The three regions I would need for this encounter table would be "outer Rim", "High Ice", and "Boreal Expanse".

I apologize now for my ignorance.
 
sectionIdentifier no longer seems to be used. i think i had sectionIdentifier for something else... dont remember what...i can look it up.

Change sectionIdentifier to "regionNumber" in all places, and it should work fine for you.

if you want one that works on names, i can do that when i get home, but that wont be for 2+ hours (im currently typing on a mobile device)
 
function rand(x) {
return Math.ceil(Math.random() * x);
}

Its the Math.ceil that makes it from 1-100 as opposed 0-99
Math.ceil makes it 0-100 instead of 0-99. Math.random() returns [0-1), so you can (extremely rarely) get 0 and (extremely slightly less rarely than desired) get 100.

You would want
Code:
Math.floor(Math.random() * x) + 1;
to get a number from 1-x.

OK, back to Gen Con for me! :)
 
Bad code on my part. sectionIdentifier not used.

to make it more configurable on your part:

make the function definition like this:

Code:
function tableResults(argument, rollModifier) {

make the randomizer like this:

Code:
    if (!rollModifier) {
       rollModifier = 0;
    }
    var randomNum = rand(100) + rollModifier;


make your HTML like this:

Code:
<form>
<input type="button" name="Button1" value="Outer Rim" onClick="var x = tableResults('varisia', 0);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button2" value="High Ice" onClick="var x = tableResults('varisia', 25);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button3" value="Boreal Expanse" onClick="var x = tableResults('varisia', 50);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>
 
Although I can only rely on the randomness. It appears to be working. Outer Rim gets results of 1-100, High Ice 26-125, and Boreal Expanse 51-150. I did not change sectionIdentifier to regionNumber.
 
fair enough. my previous post right before yours will make it so you can control the number a little bit more as well. up to you what you prefer.
 
Math.ceil makes it 0-100 instead of 0-99. Math.random() returns [0-1), so you can (extremely rarely) get 0 and (extremely slightly less rarely than desired) get 100.

You would want
Code:
Math.floor(Math.random() * x) + 1;
to get a number from 1-x.

OK, back to Gen Con for me! :)

parody is absolutely correct.

my bad.

so the function should be:
Code:
function rand(x) {
   return Math.floor(Math.random() * x) + 1;
}
 
Last edited:
Would I replace all of this

var randomNum = rand(100) + ((sectionIdentifier -1) * 25)
var arrayOfChoices = allArrays[argument];
var error = "";
if (!arrayOfChoices) {
error = "no such set: " + argument;
}


with

if (!rollModifier) {
rollModifier = 0;
}
var randomNum = rand(100) + rollModifier;
 
no. You would replace:

var randomNum = rand(100) + ((sectionIdentifier -1) * 25)


with

if (!rollModifier) {
rollModifier = 0;
}
var randomNum = rand(100) + rollModifier;


-----------------------------------------------------------------

leave the arrayOfChoices section alone.
 
Crap I seem to have broke it. This is what I currently have.


<HTML>
<HEAD>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Crown of the World Caravan Encounters</TITLE>
</HEAD>
<BODY>

<script language="JavaScript">

// Some Arrays


rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
}

function rand(number) {
return Math.ceil(rnd()*number);
}

function tableResults(argument, rollModifier) {

var varisiaArray = [];

varisiaArray.push({"max": 5, "name": "Rough Terrain", "content": "a" });
varisiaArray.push({"max": 10, "name": "Cabin", "content": "b" });
varisiaArray.push({"max": 15, "name": "Erutaki Encampment", "content": "c" });
varisiaArray.push({"max": 20, "name": "Hungry Predators", "content": "d" });
varisiaArray.push({"max": 25, "name": "Horned Herd", "content": "e" });
varisiaArray.push({"max": 30, "name": "Wanderers", "content": "f" });
varisiaArray.push({"max": 35, "name": "Wrecked Caravan", "content": "g" });
varisiaArray.push({"max": 40, "name": "Dragon Sighting", "content": "h" });
varisiaArray.push({"max": 50, "name": "Blizzard", "content": "A bitter storm of driving wind and snow engulfs the caravan for 1d4 days. During this time, the caravan takes a –2 penalty to AC and on Attack and Security checks because of poor visibility and hostile weather. The caravan can make a DC 22 Security check each day to move at half speed during the blizzard. If the caravan fails the check by 4 or less, the caravan moves at one-quarter speed. If the caravan fails the check by 5 or more, it makes no progress. Casting control winds grants a +2 bonus on this check, while control weather grants a +4 bonus. When the blizzard ends, the deep drifts of snow it leaves behind are considered a Rough Terrain encounter (see page 17) for 1d4 days. XP Award: Award the PCs 3,200 XP for a blizzard encounter." });
varisiaArray.push({"max": 55, "name": "Aurora", "content": "The wondrous polar aurora ripples overhead at night for 1 week. This good omen grants the caravan a +2 bonus on Resolve checks for the duration of the aurora, and the caravan’s fortune-teller can use her reroll ability one additional time that week." });
varisiaArray.push({"max": 60, "name": "Wings of Hope", "content": "k" });
varisiaArray.push({"max": 65, "name": "Death from Below", "content": "l" });
varisiaArray.push({"max": 70, "name": "Creeping Rot", "content": "m" });
varisiaArray.push({"max": 76, "name": "Wagon Damage", "content": "n" });
varisiaArray.push({"max": 80, "name": "Ice Hunters", "content": "o" });
varisiaArray.push({"max": 85, "name": "Pleasant Weather", "content": "p" });
varisiaArray.push({"max": 90, "name": "Polar Pudding", "content": "q" });
varisiaArray.push({"max": 100, "name": "Frozen Dead", "content": "r" });
varisiaArray.push({"max": 105, "name": "Black Monolith", "content": "The caravan approaches a monolith of black stone that rises out of the ice to a height of 20 feet. This is one of the monoliths Katiyana has been placing across the Crown of the World to cement her control over the morozkos. Creatures: A Frozen Dead encounter always accompanies the discovery of a black monolith. In addition, there is a 50% chance that it is accompanied by a Blizzard encounter (see the next column)." });
varisiaArray.push({"max": 110, "name": "The Lonely Maiden", "content": "t" });
varisiaArray.push({"max": 115, "name": "Northern Lights", "content": "u" });
varisiaArray.push({"max": 120, "name": "Rough Terrain", "content": "v" });
varisiaArray.push({"max": 125, "name": "Crevasse", "content": "w" });
varisiaArray.push({"max": 130, "name": "Polar Mirage", "content": "x" });
varisiaArray.push({"max": 135, "name": "Black Slush", "content": "The caravan encounters a wide area of tainted snow and polluted eff luent. This acts as an open Crevasse encounter (see page 12), though casting water walk grants a +2 bonus on Security checks to cross the area. In addition, the caravan must make a DC 23 Security check once per hour while crossing or become poisoned by the toxic slurry, taking a cumulative –1 penalty to AC and on Attack checks for each failed check. These penalties can be removed with a successful DC 23 Security check when the caravan rests. Each healer in the caravan grants a cumulative +1 bonus on this check, and each casting of neutralize poison grants a +2 bonus on the check. Creatures: In addition, there is a 50% chance that an area of black slush is the hunting ground of a mated pair of chardas, xenophobic monstrous humanoids from the depths of the earth, who attack the caravan (AC 23; hp 115; Attack +18; Damage 8d8+4). The chardas can be avoided with two successful DC 19 Security checks. The chardas fight to the death. Treasure: The chardas maintain a lair in an ice cave below the black slush, which can be found with a DC 25 Survival check. Inside the lair are 34 uncut diamonds (worth 100 gp each), one of which is actually an air elemental gem. XP Award: Award the PCs 3,200 XP for a black slush encounter, and an additional 6,400 XP if they defeat the mated pair of chardas." });
varisiaArray.push({"max": 140, "name": "Auroral Flight", "content": "Creature: The caravan sees a strange, blue-green light on the northern horizon, sometimes streaking at high speed in straight lines, and at other times soaring in broad circles. This is a boreal yrthak, a f lying reptile that has adapted to life in the cold skies above the frozen wastes of the Crown of the World. The caravan can make a DC 24 Security check to avoid the boreal yrthak; otherwise, the creature notices the caravan and swoops in to attack, targeting individual creatures rather than the caravan itself." });
varisiaArray.push({"max": 145, "name": "Cold Gate", "content": "aa" });
varisiaArray.push({"max": 150, "name": "Hunting Party", "content": "ab" });



var allArrays = {
"varisia": varisiaArray,
};

if (!rollModifier) {
rollModifier = 0;
}
var randomNum = rand(100) + rollModifier;
var arrayOfChoices = allArrays[argument];
var error = "";
if (!arrayOfChoices) {
error = "no such set: " + argument;
}

var content = "";
var name = "";
for (var itemIndex in arrayOfChoices) {
var item = arrayOfChoices[itemIndex];
if (randomNum <= item.max) {
content = item.content;
name = item.name
break;
}
}

var results = [];
if (error) {
results.name = "";
results.content = error;
} else {
results.name = name;
results.content = content;
}
return results;

}

</script>

<div id="error"></div>
<form>
<input type="button" name="Button1" value="Outer Rim" onClick="var x = tableResults('varisia', 0);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button2" value="High Ice" onClick="var x = tableResults('varisia', 25);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>

<form>
<input type="button" name="Button3" value="Boreal Expanse" onClick="var x = tableResults('varisia', 50);this.form.name.value=x.nam e;this.form.content.value = x.content;" size="" />
<input name="name" style="font-weight:bold;"></input>
<textarea name="content" cols="50" rows="11" wrap="soft"></textarea>

</form>
 
I look at it in like 45 minutes.

Edit: off the bat, looks like you have a typo. You have a this.form.name.value = x.nam e

It should be x.name

(You have a space misspelling the word name)

Just a guess, as I'm currently on a bus.
 
Last edited:
Back
Top