[UNITY]2DRPG開発日誌 #37 前回と異なるランダム数値を取得

こう!

 

int maxInt = 999; //ランダムの最大値

int currentInt = UnityEngine.Random.Range(0, maxInt); //前回のランダム数値
int NextInt = RandomInt(currentInt,maxInt); //前回と異なるランダム数値

   int RandomInt(int currentInt,int maxInt)
   {
      int i = 0;

      while(true) //無限ループ開始
      {
         if (maxInt == 1) return 1; //最大値が1だったら1しかないので終了

         i = UnityEngine.Random.Range(0, maxInt); //ランダム数値を設定

         if (i == currentInt) continue; //前回と同じだった場合のみ再ループ

         break; //ループ脱出
      }

      return i; //回答
   }
UNITY
スポンサーリンク
Share
Follow Us
KITTYPOOL