8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity] 確率判定

Last updated at Posted at 2019-02-15

RPGとかなんやらで「何%の確率で何かが起きる」みたいな時に使える関数があると便利だと思って書いておきます。

ソース

/// <summary>
/// 確率判定
/// </summary>
/// <param name="fPercent">確率 (0~100)</param>
/// <returns>当選結果 [true]当選</returns>
public static bool Probability(float fPercent)
{
    float fProbabilityRate = UnityEngine.Random.value * 100.0f;

    if(fPercent == 100.0f && fProbabilityRate == fPercent)
    {
        return true;
    }
    else if (fProbabilityRate < fPercent)
    {
        return true;
    }
    else
    {
        return false;
    }
}

使い方

if(Probability(30))
{
    //30%の確率で起こるイベント
}

他にも方法はいろいろあると思うけど、自分はこれが使い勝手良かった。

8
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?