random number generator

Posted By: Ruben

random number generator - 03/01/17 20:57

I am trying to create a random number generator that will create a random number every time a dragon needs to decide what direction to run, once the player gets within a certain distance of it. Depending on what the random number that gets generated, the dragon will run to a certain vector location, once the player gets within a certain close distance to the dragon.

Here is the code I have to try and make this happen:

Code:
action dragon_action() 
{  
   ...

   rndmIntGenerator = integer(random(4)); // Generate random
      //    number between 0 to 3, and store it in variable.

   ...

   while (my.status != dead)
   {
      ...

      dragnPlyrDist = vec_dist (hero.x, my.x); // Distance in
         //    units between dragon and player.

      ...

      if(my.status == dragonBiteAttack) // Dragon tries to bite
         //    player.
      {
         ...

         if (dragnPlyrDist <= 200) // (Distance Between Dragon 
                                   //    and Player) <= 200
         {
            my.status = dragonRetreatFromPlayer; // Dragon
               //    retreats from player.
         }

         ...
      }

      if(my.status == dragonRetreatFromPlayer)
      {
         ent_animate(my, "walk", run_percentage, ANM_CYCLE); 
         run_percentage -= 6 * time_step;

         if(rndmIntGenerator == 0)
	 {
	    ent_moveto(me, vector(-3, -592, -103), 30);
               // If random number is 0, dragon moves to this
               //    vector location.
	 }
			
	 if(rndmIntGenerator == 1)
	 {
	    ent_moveto(me, vector(-599, 46, -103), 30);
               // If random number is 1, dragon moves to this
               //    vector location.
	 }
			
	 if(rndmIntGenerator == 2)
	 {
	    ent_moveto(me, vector(-54, 642, -103), 30);
               // If random number is 2, dragon moves to this
               //    vector location.
	 }
			
	 if(rndmIntGenerator == 3)
	 {
	    ent_moveto(me, vector(665, 57, -103), 30);
               // If random number is 3, dragon moves to this
               //    vector location.
	 }
      }
      
      ...
   }
}



The random number generator is generating a number, and the dragon moves to that number vector location, but the number never changes, when I continually move the player to within 200 units of it in the same game. The random number stays the same, and the dragon just keeps moving to the same spot, instead of a different spot every time I get the player to within 200 units of it. If I restart the game, then the random number generator will generate a new number again, but then it stays the same for the remainder of that game.

Does anyone have advice on how I can make it so that a random number gets generated each time the player gets to within 200 units of the dragon, in the same game? In other words, the random number can change each subsequent time the player gets to within 200 units of the dragon, instead of repeating the same value over and over, within the same game?

Any help would be appreciated. Thank you.
Posted By: WretchedSid

Re: random number generator - 03/01/17 21:57

Quote:
The random number generator is generating a number, and the dragon moves to that number vector location, but the number never changes


I will probably come across as a massive dick, but... You do realize that that's due to you never changing the variable ever?

Where you assign the `dragonRetreatFromPlayer` state, you can simply generate a new random number.
Posted By: Ruben

Re: random number generator - 03/02/17 01:06

Originally Posted By: WretchedSid
Quote:
The random number generator is generating a number, and the dragon moves to that number vector location, but the number never changes


I will probably come across as a massive dick, but... You do realize that that's due to you never changing the variable ever?

Where you assign the `dragonRetreatFromPlayer` state, you can simply generate a new random number.

I tried that before. However, when I call the random number in the "dragonRetreatFromPlayer" state, and the player gets to within 200 units of the dragon, the dragon keeps continually trying to move to the different vector locations, just as if the random number is constantly being called and changed in real time, and the dragon is trying to follow the direction of the random number that keeps changing. The dragon never actually gets to a location. It just keeps wiggling here and there in the middle of the room without going anywhere.
Posted By: Ruben

Re: random number generator - 03/02/17 03:18

I figured it out. I made each instance of the random number vector location into its own individual my.status. By doing that, once the random number is generated inside the "dragonRetreatFromPlayer" state, if it is any particular random number, it will make my.status equal the status of the individual vector location to travel. It therefore knocks out of the "dragonRetreatFromPlayer" state, and goes to the new my.state that tells the program to move the dragon to a particular location.
© 2024 lite-C Forums