Enquirer Home Page | Twitter | Back to Improbable Island

Programming Tutorial XI - On The Toss Of A Coin

It's a tense moment. The joker in front of you is tossing a coin. Heads and you get to go where you want to, tails on the other hand...well.

Your guests are presented with a rickety looking rope bridge spanning a chasm with a fast flowing river at the bottom of it. If they try to cross, will the bridge hold or won't it?

There are many situations where you would like to introduce uncertainty and chance. How do you go about doing this? It requires the use of the Dice Roll Contrivance. We'll use the second scenario: "rope bridge over the chasm", as our example.

Decorate a room with your scene, such as "Approach a rickety bridge over a chasm." "Looks dodgy here." etc.etc. Then have another room connected to it, with the door labeled "Attempt to cross the chasm." Create two more rooms, one for each of the possible outcomes, maybe: "The other side of the chasm" and "AAAAAGGGGGHHHHH!!!!!". Now get a 'Dice Roll' Contrivance and set the parameter to 500. Then take two 'Warp to Room' Contraptions, one set to each of the outcome rooms that you just created. Arrange as follows, with the Warp pointing to "Make it across" in the second row and the Warp pointing to "Fall in the river." in the escape hatch of the first row.

 You have a 50/50 chance of making it across.

And there we have it.

Note: This doesn't have to go in a separate room, putting it on its own page will work equally well. The only difference is where the link to click on will be in the side bar.

Note: The above program generates a 50:50 chance of each outcome. This may not be what you wanted, maybe most of the time the bridge holds. Eighty percent of the time say you can safely cross the bridge.

The Dice Roll Contrivance picks a random number between 1 and 1000 and compares it to the parameter that you've set. If the random number is higher than the parameter, then the dice roll passes, otherwise it fails.

So for the above example (an 80% chance of crossing the bridge) you would need to set the Dice Roll Contrivance parameter to 200, since 80% of the range 1 - 1000 is above 200.

Note for budding programmers.

Nothing much to say here, the example says it all really. This is just slipped in to demonstrate the Dice Roll Contraption. Oh, wait, yes, there is: note that the Contraption checks to see if the random number is above the given number, so to give yourself an 80% chance of the Contraption passing, set the parameter to 200. For a 20% chance set it to 800. And so on. So 200 gives youa 20% chance of failure and a setting of 700 gives you a 70% chance of failure.

And that's pretty much it for this tutorial series. You should now have a basic idea about how programs work and be able to tailor your own for your own Place. Don't panic! There's one more section available: "What to do if your program doesn't work as expected".

Return to previous tutorial.
Tutorial Contents Page
Return to Introduction to Programming.
Return to Places Overview

Logged in as: Guest (Guest)
places/progtutxi.txt · Last modified: 2017/05/28 03:54 (external edit)