I am trying to make it so that the game will randomly decide which of 5 different enemy NPC's to give a key to, so if the player kills the enemy and loots its body, it will find the key that will unlock the door out of the room.

As of now, the random() function seems to be generating a number, but it always chooses the same number (2) every time I restart the game. The number never changes.

This is the code I have that is making this happen:
Code:
...

int whoHasHallwayKey;
var keyInserted;

...

action player_code()
{
   ...

   whoHasHallwayKey = random(5); // Always chooses 2, every
      //    time I start game.  Never changes.

   while(player_health > 0)
   {
      ...

      if((whoHasHallwayKey == 0) && 
         (keyInserted == 0))
      {
         inv_insert_item_into_bag(bag_goblin, 
            item_key1_goblin);
	 keyInserted = 1;
      }
      if((whoHasHallwayKey == 1) && 
         (keyInserted == 0))
      {
         inv_insert_item_into_bag(bag_monster, 
            item_key_monster);
	 keyInserted = 1;	
      }
      if((whoHasHallwayKey == 2) && 
         (keyInserted == 0)) // This is always initiated, for
                             //    some reason.
      {
         inv_insert_item_into_bag(bag_wizard, 
            item_key_wizard);
	 keyInserted = 1;	
      }
      if((whoHasHallwayKey == 3) && 
         (keyInserted == 0))
      {
         inv_insert_item_into_bag(bag_knight, 
            item_key_knight);	
	 keyInserted = 1;
      }
      if((whoHasHallwayKey == 4) && 
         (keyInserted == 0))
      {
         inv_insert_item_into_bag(bag_giantScorpion, 
            item_key1_giantScorpion);	
	 keyInserted = 1;
      }

      ...
      
      wait(1);
   }
}

function main()
{
   ...

   keyInserted = 0;

   ...
}



The item is being inserted into the NPC's inventory bag using the inv_insert_item_into_bag() function. However, the whoHasHallwayKey variable is always being set to 2 every time I run the game, and never changes from this value, thus making it so the wizard always gets the key, and no one else, every game.

Does anyone know why my random number does not appear to change randomly every time I run the game, and just keeps the same value (2) for every game run over and over?

Last edited by Ruben; 03/22/17 07:17.