Ok.
This is a tutorial that you all should like, its todo with scripting but were gonna make something real cool.
Say, in your game, you have a bridge and a mean ugly looking troll guarding the bridge.
The troll activites when you attempt to walk over the bridge, and lets you pass if you get a pop quiz right. :)

What is the currancy of gold in Frey called:-
 > (A) Dollars
   (B) Pounds
   (C) Riai

So we have a question, three possible answers and a correct answer.
This is also made nice via a menu object so that you can easily select your answer.

What we are going to do is make a function called Troll and add a trigger to that function on the map where the bridge is.
So in the map editor, right click on the bridge...
Goto:- Insert Entity > Trigger.
And when the dialog box appears fill in like so:-
Troll();

Then click OK.

Save the map...
File > Save.

Ok, now your ready to open or make troll.js and script the function Troll...
File > New > Script.
File > Save As.

In the Save As dialog make sure the you save this file as troll.js and put it in your games script directory.
So you should have something like:- "C:/sphere/games/yourgame/scripts/troll.js"

Ok, now you can start writing your function.
The basic idea is we make a random number function and use it to select questions from an array of a custom object called question.
First how to generate random numbers...

In SphereScript we can use JavaScript's Math.random() method.
Which returns a random number inbetween 0 and 1, e.g. 0.5315235353453
So we times this number by the amount of questions we have and round it off to a whole number.
Like so:- Math.floor(Math.random()*num_questions);

Math.floor returns the lowest number equal to or lower than the number, e.g. 7.4 becomes 7.
Ok, so now we can easily script a function for that...

function random(low, high)
{
  return low + Math.floor(Math.random()*high - low + 1);
}

Ok.
Now how to make an array.
An array is simply an collection of variables.
var names = new Array();
names[0] = "Fred";
names[1] = "Jim";
names[2] = "John";

The number of elements in this array are located at the length property.
So the names array has an length of 3.

Now we must learn how to make objects...

function question(lquestion, lone, ltwo, lthree, lcorrect)
{
  this.question = lquestion;
  this.correct = lcorrect;
  this.one = lone; 
  this.two = ltwo;
  this.three = lthree;
}

And to make an instance of this object we write the line:-
var q1 = new question("How much is a sword at Frey's shop?","30","50","200",2);
Ok.
Now we've made a q1 object with the properties
question
correct
one
two
three

So we can access those properties by putting the name of our variable, and the accesor method or a "." and the property name...
var q1 = new question("How much is a sword at Frey's shop?","30","50","200",2);
GetSystemFont().drawText(0,0, q1.question);

Ok, now we put all our questions in an array:-

var questions = new Array();
questions[0] = new question("How much is a sword at Frey's shop?","30","50","200",2);
questions[1] = new question("How many houses are in Frey?","1","2","50",0);
questions[2] = new question("What is the lakes name in Frey?","Iowa","Ativia","New York",1);
var choosen_yet = new Array();
for(var i = 0; i < all_questions.length; ++i)
{
  choosen_yet[i] = false;
}
  
Also, we added a parrallel choosen_yet array with all the elements as false.
This is so we can check if a question is already choosen in our 3 round quiz.
Ok, now here we go, this is the whole quiz plus using the menu object.

/////////////////////////////////////////////////////////////////////////////////////////////////////////
EvaluateSystemScript("flik_menu.js"); // find it on my files page
EvaluateSystemScript("screen.js");
EvaluateSystemScript("oldsphere.js");

function Wait()
{
  while(AreKeysLeft()) GetKey();
}

function game()
{
  Troll();
}

function question()
{
  this.question = question.arguments[0];
  this.answers = new Array();
  for (var i = 1; i < question.arguments.length - 1; ++i)
    this.answers.push(question.arguments[i]);
  this.correct = question.arguments[question.arguments.length - 1];
}

function random(low, high)
{
  return low + Math.floor(Math.random()*high - low + 1);
}

function Troll()
{
  var total_right = 0;

  var questions = new Array();
  questions.push(new question("How much is a sword at Frey's shop?","30","50","200",2));
  questions.push(new question("How many houses are in Frey?","1","2","50",0));
  questions.push(new question("Who made this tutorial...","Fenix","Flik","Aegis",1));
  questions.push(new question("What does the troll look like:-","Ugly","Purple","Green",0));
  questions.push(new question("What is Flik's email address?","vascy@hotmail.com","hello@something.com","duhserv@esper.net",0));
  questions.push(new question("How many bits in a byte?","38","8","dunno",1));
  questions.push(new question("Can Flik think of good questions for a pop quiz?","Yes","No","Maybe",2));

  var choosen_yet = new Array();
  for(var i = 0; i < questions.length; ++i)
  {
    choosen_yet[i] = false;
  }

  var round = 0;
  while(round < 3)
  {
    var number = -1;
    // choose a question
    while(number < 0)
    {
      var temp = random(0, questions.length - 1);
      if(choosen_yet[temp] == false)
      {
        choosen_yet[temp] = true;
        number = temp;
      }
    }

    var troll_menu = new Menu(32, 48, GetScreenWidth() - 64, 74);
    troll_menu.preRender = function()
    {
      GetSystemFont().drawText(0,0, questions[number].question);
      GetSystemWindowStyle().drawWindow(troll_menu.x, troll_menu.y, troll_menu.getWidth(), troll_menu.getHeight());
    }

    for (var i = 0; i < questions[number].answers.length; ++i)
      troll_menu.addText(questions[number].answers[i], function(){ 
        if(troll_menu.selection == questions[number].correct)
        { 
          GetSystemFont().drawText(0,16, "Correct!");
          ++total_right;
        }
        else
          GetSystemFont().drawText(0,16, "Incorrect!");
      troll_menu.done = true;
      FlipScreen();
      Wait();
      ++round;
    });

    troll_menu.execute();
 
  }

  if(total_right == 3)
    GetSystemFont().drawText(0,0, "You may pass...");
  else
    GetSystemFont().drawText(0,0, "You may not pass...");
  FlipScreen();
  Delay(2000);
}


Uhh, that's horrible looking code that makes me cry!
Please help! Ok...

function Quiz(questions, max_questions, num_needed)
{
  this.questions = questions;
  this.max_questions = max_questions;
  this.num_needed = num_needed;

}

Quiz.prototype.play = function()
{
  var round = 0;
  var current_total = 0;

  var choosen_yet = new Array();
  for(var i = 0; i < this.questions.length; ++i)
    choosen_yet[i] = false;

  while(round < this.max_questions)
  {
    var number = -1;
    // choose a question
    while(number < 0)
    {
      var temp = random(0, this.questions.length - 1);
      if(choosen_yet[temp] == false)
      {
        choosen_yet[temp] = true;
        number = temp;
      }
    }

    var menu = new Menu(32, 48, GetScreenWidth() - 64, 74);
    troll_menu.preRender = function()
    {
      // this is where'd you add in a picture of the quiz master? ^^;
      GetSystemFont().drawText(0,0, this.questions[number].question);
      GetSystemWindowStyle().drawWindow(menu.x, menu.y, menu.getWidth(), menu.getHeight());
    }

    for (var i = 0; i < this.questions[number].answers.length; ++i)
      menu.addText(this.questions[number].answers[i], function(){ 
        if(menu.selection == this.questions[number].correct)
        { 
          GetSystemFont().drawText(0,16, "Correct!");
          ++total_right;
        }
        else
          GetSystemFont().drawText(0,16, "Incorrect!");
      menu.done = true;
      FlipScreen();
      Wait();
      ++this.round;
    });

    menu.execute();
 
  }

  if(current_total == this.num_needed)
    GetSystemFont().drawText(0,0, "You may pass...");
  else
    GetSystemFont().drawText(0,0, "You may not pass...");
  FlipScreen();
  Delay(2000);
}

function Troll()
{
  var questions = new Array();
  questions.push(new Question("What is something?", "1", "A word", 1));
  questions.push(new Question("Yay?", "Yes", "No", 0));
  questions.push(new Question("2 + 2 is?", "4", "A very difficult question indeed", "8 probably", 0));

  var troll_quiz = new Quiz(questions, 3, 2);
  troll_quiz.play();
}

You've made it more complex, argh.. I hate you!!
Heheh, no. I've given the quiz it's own object. So now if we needed a second quiz, we wouldn't have to type out the whole thing again.
We'd just use our quiz interface, which is kind of like a wrapper for the question object.


Ok, thats it, enjoy.
Anyhow. That is an okay tutorial which covers many things, thanks.