Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Random number not being random #464979
03/22/17 07:16
03/22/17 07:16
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
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.
Re: Random number not being random [Re: Ruben] #464980
03/22/17 07:32
03/22/17 07:32
Joined: Oct 2008
Posts: 681
Germany
Ayumi Offline
User
Ayumi  Offline
User

Joined: Oct 2008
Posts: 681
Germany
random_seed(0);

Re: Random number not being random [Re: Ayumi] #464987
03/22/17 14:41
03/22/17 14:41
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
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.

Last edited by Ruben; 03/22/17 14:44.
Re: Random number not being random [Re: Ruben] #464988
03/22/17 15:07
03/22/17 15:07
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Call `random_seed(0)` at start up. Then call random() in your action.

[Insert discussion about randomness here]


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Random number not being random [Re: WretchedSid] #464989
03/22/17 15:53
03/22/17 15:53
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
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().

Re: Random number not being random [Re: txesmi] #464990
03/23/17 02:24
03/23/17 02:24
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
Cool. Thank you all! It worked! I really appreciate it. :-)


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1