Random number not being random

Posted By: Ruben

Random number not being random - 03/22/17 07:16

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?
Posted By: Ayumi

Re: Random number not being random - 03/22/17 07:32

random_seed(0);
Posted By: Ruben

Re: Random number not being random - 03/22/17 14:41

I tried using random_seed(0), but it also keeps giving me the same random number of "123", every time I run the game, which never changes. There are only 5 NPC's, so I was hoping for only a range of 0-4 (5 NPC's). With the value "123", none of the 5 NPC's gets the key, using the above code.
Posted By: WretchedSid

Re: Random number not being random - 03/22/17 15:07

Call `random_seed(0)` at start up. Then call random() in your action.

[Insert discussion about randomness here]
Posted By: txesmi

Re: Random number not being random - 03/22/17 15:53

When all else fails... read the instructions.

Originally Posted By: random in the manual
The var returned is fractional, and is always less than the upper limit. The random sequence starts always with the same numbers, unless 'randomized' at game start by random_seed().
Posted By: Ruben

Re: Random number not being random - 03/23/17 02:24

Cool. Thank you all! It worked! I really appreciate it. :-)
© 2024 lite-C Forums